$wpdb->last_error and $wpdb->hide_errors()

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

$wpdb->last_error and $wpdb->hide_errors()

Haluk Karamete
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
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: $wpdb->last_error and $wpdb->hide_errors()

J.D. Grimes
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 <[hidden email]> 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
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers

_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers