wpdb last_query, last_result, last_error

939

There are times that you wanted to print the exact query itself or query result right after it is executed. Use following wpdb class variables to troubleshoot.

$wpdb->last_query

This prints that last query that is executed, I mean the exact MySQL Query itself.

$laundrycustomer = $wpdb->get_row(“SELECT company_name, customer_name, phone, address FROM laundry_customers where phone = $searchphone;”);

echo “<br>Query executed is “.$wpdb->last_query;

The ouput of above is

Query executed is SELECT company_name, customer_name, phone, address FROM laundry_customers where phone = 34343456;

 

$wpdb->last_result

This prints the last query results

$laundrycustomer = $wpdb->get_row(“SELECT company_name, customer_name, phone, address FROM laundry_customers where phone = $searchphone;”);

echo “<br>Result is “.$wpdb->last_result;

The output of above is

Result is Array

 

$wpdb->last_error

This prints the last error text generated by MySQL.

$laundrycustomer = $wpdb->get_row(“SELECT company_name, customer_name, phone, address FROM laundry_customers where phone = $searchphone;”);

echo “<br>Error is “.$wpdb->last_error;

The output of above is, assume that I have entered junk of symbols in $searchphone variable, then

Error is You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘.\’;.[‘ at line 1

· ·


Related Articles & Comments

Thank you for visiting us. To continue receiving updates, please Subscribe to our Social Media Channels.

Google+
FACEBOOK
RSS
YOUTUBE
Menu Title
%d bloggers like this: