I am searching for a way by which i can print the executed sql query just after the :

                                INTO tbl_watchprosite SET 

This would be great if i can see what values are going in the query.


  • 1
    I know it's too late, but for future reference. You can just echo prepare statement before passing it to query. It would be surely easier. – Maciej Paprocki Oct 20 '16 at 17:08

The $wpdb object has some properties getting set for that:

// Print last SQL query string
// Print last SQL query result
// Print last SQL query Error

Note, that you will only get the result when you have set defined( 'SAVEQUERIES', true ); in your wp-config.php.

  • hmm but in my case there is nothing in $wpdb->last_query. – ravisoni Aug 16 '13 at 11:11
  • Have you defined( 'SAVEQUERIES', true ); in your wp-config.php or something like ! defined( 'SAVEQUERIES' ) AND defined( 'SAVEQUERIES', true ); in your script? Else it won't work. – kaiser Aug 16 '13 at 11:32
  • Yes i have, I think the query is not running at all that y there is nothing setting is $wpdb->last_query. :( – ravisoni Aug 16 '13 at 11:36
  • 1
    turn on wp_debug then, so that you'll get errors or warning if any there. – sven Aug 16 '13 at 11:38
  • WordPress database error: [Query was empty] – ravisoni Aug 16 '13 at 11:45

Add this in your wp-config.php

 define('SAVEQUERIES', true);

Then in the footer of your theme put this:

  if (current_user_can('administrator')){
   global $wpdb;
   echo "<pre>";
   echo "</pre>";
 }//Lists all the queries executed on your page

Also you can use

global $wpdb;
echo $wpdb->last_query;//lists only single query

in your query page after your query;

  • How to get ajax initiated queries? – itsazzad Sep 18 '15 at 11:36
  • you can print the same in the ajax action handler function. – sven Sep 18 '15 at 15:56

You have to add both functions,otherwise it will never show error


This function will show you proper error like this this

enter image description here


I wanted to add that the best up-voted answer by @kaiser is not fully correct:

// Print last SQL query string

The return of it is ARRAY, not a string. So to output last query you should do this:

echo 'Last query: '.var_export($wpdb->last_query, TRUE);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.