My query is working below, except I can't display the results which should be "2" (the results of the distinct count). Instead I get "Total Authors: ID"
//var_dump Result:
array(1) {
[0]=> object(stdClass)#349 (1) {
["COUNT(DISTINCT writing.ID)"]=> string(1) “2″
}
}
//Code:
$authors = $wpdb->get_results("SELECT COUNT(DISTINCT writing.ID)
FROM writing
LEFT JOIN stories on writing.SID = stories.SID
LEFT JOIN wp_users ON writing.ID = wp_users.ID
WHERE (stories.SID = $the_SID)"
);
echo var_dump($authors);
print "Total Authors:" .$authors[0]->writing.ID ."<p>";
As you can see from the output of var_dump
, your required value is being stored as a member COUNT(DISTINCT writing.ID)
of the object. There are a few workarounds you can follow.
Store the index as a separate variable and then use it when printing.
$t = "COUNT(DISTINCT writing.ID)"; print "Total Authors:" .$authors[0]->$t . "<p>";
Use an alias in the MySQL query.
SELECT COUNT(DISTINCT writing.ID) AS writingID FROM writing LEFT JOIN stories ON writing.SID = stories.SID LEFT JOIN wp_users ON writing.ID = wp_users.ID WHERE (stories.SID = $the_SID)
and then,
print "Total Authors:" .$authors[0]->writingID . "<p>";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.