How to print the excuted sql right after its execution

  • 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

    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.

  • kaiser

    kaiser Correct answer

    7 years ago

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

    global $wpdb;
    
    // Print last SQL query string
    $wpdb->last_query
    // Print last SQL query result
    $wpdb->last_result
    // Print last SQL query Error
    $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.

    hmm but in my case there is nothing in $wpdb->last_query.

    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.

    Yes i have, I think the query is not running at all that y there is nothing setting is $wpdb->last_query. :(

    turn on wp_debug then, so that you'll get errors or warning if any there.

    WordPress database error: [Query was empty]

    Try `$wpdb->get_results()` instead and take a look at the Codex documentation on `$wpdb`.

    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.

    @BrianC You might want to link to the patch? Or even better: Add it in an answer? Or [edit] this answer?

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM