Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select count, sum using PHP PDO

How can I get count(*) or SUM(col1) using PHP PDO

I have used

$STH_SELECT = $dbh->prepare("SELECT count(*) FROM Table");
$STH_SELECT->execute;
$result = $STH_SELECT->fetchAll;
$Count = $result[0][0];

Is this correct and Is there a better way?

like image 294
cldy1020 Avatar asked Sep 03 '26 10:09

cldy1020


1 Answers

You don't need to use prepared statement when you aren't entering any data into the query.

$STH_SELECT = $dbh->query("SELECT count(*) FROM Table");
$Count = $STH_SELECT->fetchColumn();
like image 157
Cameron Martin Avatar answered Sep 06 '26 04:09

Cameron Martin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!