[wp-hackers] $wpdb->last_error and $wpdb->hide_errors()

J.D. Grimes jdg at codesymphony.co
Mon Jul 14 13:09:45 UTC 2014


This works for me. If you error_log( $wpdb->last_error ) you will see something like: 

> [14-Jul-2014 13:03:56 UTC] WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SHOWasadads TABLES' at line 1 for query SHOWasadads TABLES  made by blp_get_tables

-J.D.

On Jul 13, 2014, at 9:53 PM, Haluk Karamete <halukkaramete at gmail.com> wrote:

> I wrote this function to get a list of all the tables in the current
> wordpress database.
> 
> Though I commented in the code what does not work, I brief it here.
> 
> as you see, I start the show with the $wpdb->hide_errors();
> then I create a deliberate SQL error;
> instead of running the SHOW TABLES, I run SHOW323234 TABLES.
> 
> Because of the wpdb->hide_errors(), I get silence.
> That's fine and expected.
> 
> But why is it that at that time, our $wpdb->last_error produces NADA?
> 
> 
> function blp_get_tables($sep = ',') {
> global $wpdb;
> 
>        $wpdb->hide_errors();
>        $wpdb->flush();
> global $blp_num_rows;
> global $blp_last_error;
> global $blp_last_sql;
> global $blp_last_query;
> 
> $retval = "";
> $sql =  "SHOWasadads TABLES ";
> $results = $wpdb->get_results($sql,'ARRAY_N');
> 
> /*
> as you see  I deliberately create a SQL error here by changing the $sql to
> something that does not make sense..
> 
> 
> at this time the $wpdb->hide_errors() is in effect,
> so I get no errors and I get an empty array into the $results..
> 
> which is totally fine and expected ...
> 
> but what puzzles me is that I do not get anything out of that
> $wpdb->last_error neither... and that drives me [brasilian] nuts.
> 
> If I were to turn on errors by replacing the code
> $wpdb->hide_errors(),
> with $wpdb->show_errors(), I get the error reported on the screen!
> 
> so why does not $wpdb->last_error() do what it's supposed to do and return
> the suppressed error (that show_errors() would have shown if given the
> chance)?
> 
> */
> 
> $blp_last_sql = $wpdb->last_query;
> 
> if ( FALSE === $results ):  # I also noticed that this is not gonna fly as
> get_results does not return a FALSE in case of a SQL error, but hey that's
> another problem... I'm more curious on the last_error thing.
> 
> $blp_last_error = $wpdb->last_error;
> else:
> $blp_last_error = '';
> endif;
> $table = '';
> foreach ($results as $current_item):
> $table .= $current_item[0] . $sep;
> endforeach;
> return $table;
> }
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list