Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

results from $wpdb->last_query

Tags:

php

wordpress

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:

    $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?

like image 883
QandA Avatar asked Nov 09 '14 23:11

QandA


People also ask

How do I find the last query in WordPress?

Print Executed SQL in WordPress To print sql queries in wordpress we use $wpdb object. The $wpdb object has some properties for this. Using $wpdb->last_query to print just the latest query executed, this is useful for debugging functions. Using a plugin like Query Monitor.

What is $Wpdb?

The $wpdb object can be used to read data from any table in the WordPress database, not just those created by WordPress itself.

What is Wpdb prepare?

wpdb::prepare( string $query, mixed $args ) Prepares a SQL query for safe execution.


2 Answers

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

like image 111
David Avatar answered Sep 17 '22 23:09

David


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

like image 38
phpdevpb Avatar answered Sep 16 '22 23:09

phpdevpb