简体   繁体   中英

Fastest way to display result of count(*) with mySQL (Preferably in one line)

There are multiple moments where I want to simply show a scalar coming back from a count(*) request but I feel like there's a one-liner I could use to replace that. Here is what I am doing :

$result = $mysqli->query("SELECT COUNT(*) FROM PRODUCTS WHERE CATEGORY='X'");
$line=$result->fetch_assoc();
echo $line["COUNT(*)"];

isn't there a way I could simply do something like

echo $result["COUNT(*)"]->fetch_assoc()->query("SELECT COUNT(*) FROM PRODUCTS WHERE CATEGORY='X'");

Thanks for your help!

If you want to do this, you need to fix your chaining. This

echo $mysqli->query("SELECT COUNT(*) FROM devtools_log")->fetch_assoc()["COUNT(*)"];

should work.

EDIT: Didn't see Sean's comment. It is a bit more extensive :)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM