admin管理员组

文章数量:1326104

I am trying to build a shortcode that will query a db and sum. Here is what I have so far:

function my_plugin_name_count_people() {
    global $wpdb;
return $wpdb->query ("SELECT COALESCE(SUM(`column_one`), column_two ,0) FROM `table_name` WHERE `booking_date` = CURDATE() -10");
}

add_shortcode('countpeople', 'my_plugin_name_count_people');

This returns '1' as the value but if I run the statement in myphpadmin then it shows NULL. That is accurate since there are no results for that day. Anyone know why it is showing '1' on the SUM() count?

I am trying to build a shortcode that will query a db and sum. Here is what I have so far:

function my_plugin_name_count_people() {
    global $wpdb;
return $wpdb->query ("SELECT COALESCE(SUM(`column_one`), column_two ,0) FROM `table_name` WHERE `booking_date` = CURDATE() -10");
}

add_shortcode('countpeople', 'my_plugin_name_count_people');

This returns '1' as the value but if I run the statement in myphpadmin then it shows NULL. That is accurate since there are no results for that day. Anyone know why it is showing '1' on the SUM() count?

Share Improve this question edited Aug 12, 2020 at 9:41 mozboz 2,6281 gold badge12 silver badges23 bronze badges asked Aug 12, 2020 at 0:51 BobBob 134 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You need to read the manual for the functions you're using as all the information for how to debug is contained there.

$wpdb-query() description is very clear. The return value is:

(int|bool) Boolean true for CREATE, ALTER, TRUNCATE and DROP queries. Number of rows affected/selected for all other queries. Boolean false on error.

As mentioned in answers to your previous questions, you probably want something like $wpdb->getvar(), which returns one scalar value from a query.

本文标签: phpwpdbgtquery returns different value to phpMyAdmin