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?
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.
The $wpdb object can be used to read data from any table in the WordPress database, not just those created by WordPress itself.
wpdb::prepare( string $query, mixed $args ) Prepares a SQL query for safe execution.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With