wpdb last_query, last_result, last_error

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

All in One WordPress Hosting
HostGator $2.75 per month
HostGator
24/7/365 Technical Support, Free Site Building Tools, 4500 Website Templates, Free Shopping Cart Software, Ideal for WordPress, 45 Day Money Back Guarantee
Starts at 30$ per month
All in One WordPress Hosting
WordPress
High optimized WordPress hosting, secure firewall, HTTPS, Backup, hack-fix guarantee and many others at 30$ per month

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