最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - wpdb->query returns different value to phpMyAdmin

programmeradmin3浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论