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?

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.