results from $wpdb->last_query

I am attempting to move a single page into WordPress as a plugin as an Admin menu. I first verify that isset($wpdb) but my test query fails. I do $wpdb->last_query and I don’t understand the results.

This is what I try:

Read More
    $clientquery = $wpdb->query("select post_title from wp_posts where id=390");
    exit(var_dump( $wpdb->last_query));

This is the result:

string(44) "select post_title from wp_posts where id=390"

Where is string(44) coming from? BTW, I am absolutely new to developing in PHP and WordPress learning this on my own.

Also, if I change the query to “select * from …” the result changes to string(35)… . What?

Related posts

Leave a Reply

2 comments

  1. assuming you have declared $wpdb as a global the above should work so your code should look like this:

    global $wpdb;
    $clientquery = $wpdb->query("select post_title from wp_posts where id=390");
    var_dump($clientquery);
    

    note the results are stored in $clientquery

  2. The $wpdb->last_query shows the exact MySQL Query itself and string(44) indicates that it is string type of length 44:

     string(44)   select post_title from wp_posts where id=390
    

    WordPress defines a class called wpdb, which contains a set of functions used to interact with a database. Its primary purpose is to provide an interface with the WordPress database, but can be used to communicate with any other appropriate database. So it is not necessary to check using isset() function