Echo returned value from mysql count (*) query

I cannot understand why this does not say anything. What I'm trying to repeat is the number of users in the db table. Any help would be greatly appreciated.

$stmt = $dbh->prepare("SELECT count(*) FROM Users");
$stmt->execute();
$result = $stmt -> fetch();
echo $result;
+3
source share
1 answer

With only one column selected, the make fetch method does not return a row. In most cases, it still returns its associative default selection array.

To access it as a column

you need to add a column alias AS
$stmt = $dbh->prepare("SELECT count(*) AS cnt FROM Users");
$stmt->execute();
$result = $stmt -> fetch();
echo $result['cnt'];
+2
source

All Articles