42

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

$wpdb->query(
                $wpdb->prepare("INSERT 
                                INTO tbl_watchprosite SET 
                                keywords=%s,url_to_post=%s,description=%s,
                                date_captured=%s,crawl_id=%d,
                                image_main=%s,images=%s,brand=%s,
                                series=%s,model=%s,condition=%s,box=%s,
                                papers=%s,year=%s,case_size=%s,status=%s,listed=%s,
                                asking_price=%s,retail_price=%s,payment_info=%s,forum_id=%d",
                                $this->getForumSettings()->search_meta,$element->href,$post_meta['description'],current_time('mysql'),$cid,$post_meta['image_main'],$images,$post_meta[0],$post_meta[1],$post_meta[2],$post_meta[3],$post_meta[4],$post_meta[5],$post_meta[6],$post_meta[7],$status,$post_meta[9],$post_meta[10],$post_meta[11],$this->getForumSettings()->ID)
            );

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

Thanks

1
  • 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. Commented Oct 20, 2016 at 17:08

5 Answers 5

91

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

global $wpdb;

// Print last SQL query string
echo $wpdb->last_query;

// Print last SQL query result
echo $wpdb->last_result;

// Print last SQL query Error
echo $wpdb->last_error;

Note: First of all you have to set define( 'SAVEQUERIES', true ); in your wp-config.php file at root folder of WordPress.

8
  • hmm but in my case there is nothing in $wpdb->last_query.
    – Ravi Soni
    Commented Aug 16, 2013 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
    Commented Aug 16, 2013 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. :(
    – Ravi Soni
    Commented Aug 16, 2013 at 11:36
  • 1
    turn on wp_debug then, so that you'll get errors or warning if any there.
    – Kumar
    Commented Aug 16, 2013 at 11:38
  • 2
    I know your original query was from a long time ago, but it sounds like you were hitting the column size issue. Just for anyone else who is searching for a solution to no query results with no error - be warned that wpdb exits silently, with no message or error, when a column in your query exceeds the size of the column in your database. There is almost no way to see this has happened, and WordPress have been carelessly resistant (IMHO) to fixing this. There is a short patch you can put into wpdb.php in a dev environment to make seeing this much easier.
    – Brian C
    Commented Dec 22, 2019 at 6:19
19

I've listed down 3 approaches in here:

  1. Using SAVEQUERIES and printing all the queries in footer
  2. Using $wpdb->last_query to print just the latest query executed, this is useful for debugging functions.
  3. Using a plugin like Query Monitor.

You'd need to add this in your wp-config.php

 define('SAVEQUERIES', true);

Then in the footer of your theme add this code:

 <?php
  if (current_user_can('administrator')){
   global $wpdb;
   echo "<pre>Query List:";
   print_r($wpdb->queries);
   echo "</pre>";
 }//Lists all the queries executed on your page
?>

Or if you'd like to print just the last executed query, you can use this just below your $wpdb query function call.

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

A 3rd approach would be to use a plugin like Query Monitor which lists all the queries executed on a page in detail, and other details associated with it like how many rows it returns and the time taken for execution or if it's a slow query. http://wordpress.org/plugins/query-monitor/

It's a good idea to use this plugin in DEV environment only and shouldn't be left activated on a live site. Also, Query Monitor can sometimes cause issues with your page, Like 5XX error on your template/page if there are too many errors.

2
  • 1
    How to get ajax initiated queries?
    – itsazzad
    Commented Sep 18, 2015 at 11:36
  • you can print the same in the ajax action handler function.
    – Kumar
    Commented Sep 18, 2015 at 15:56
4

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

$wpdb->show_errors(); 
$wpdb->print_error();

This function will show you proper error like this this

enter image description here

2

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

// Print last SQL query string
$wpdb->last_query

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);
1

Sometimes the last query is not very relevant. For example $wpdb->last_query gave me this:

SELECT wp_posts.* FROM wp_posts WHERE ID IN (410,337,350,353,352,351)

Obviously I was looking for the query before this one... To achieve this I used a query filter:

add_filter('query', function($query) {
  echo $query;
  return $query;
});

$my_query = new WP_Query($my_args);

// -> SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  WHERE 1=1  AND wp_posts.post_parent = 0  AND wp_posts.post_type IN ('attachment') AND ((wp_posts.post_status = 'inherit'))  ORDER BY wp_posts.post_type DESC, wp_posts.post_date DESC LIMIT 0, 100
// -> SELECT FOUND_ROWS()
// -> SELECT wp_posts.* FROM wp_posts WHERE ID IN (410,337,350,353,352,351)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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