简体   繁体   中英

Echo returned value from count(*) mysql query

I cannot figure out why this does not echo anything. What I am trying to echo is the amount of users in the db table. Any help would be appreciated.

$stmt = $dbh->prepare("SELECT count(*) FROM Users");
$stmt->execute();
$result = $stmt -> fetch();
echo $result;

Having only one column selected, doesn't make the fetch method to return string. In most of the cases it still returns its default fetch - associative array.

You need to alias the column with AS in order to access it as column

$stmt = $dbh->prepare("SELECT count(*) AS cnt FROM Users");
$stmt->execute();
$result = $stmt -> fetch();
echo $result['cnt'];

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