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

Haluk Karamete halukkaramete at gmail.com
Mon Jul 14 01:53:03 UTC 2014


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;
}


More information about the wp-hackers mailing list