简体   繁体   English

Echo从count(*)mysql查询返回值

[英]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. 我想要回应的是db表中的用户数量。 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. 只选择了一列,不会使fetch方法返回字符串。 In most of the cases it still returns its default fetch - associative array. 在大多数情况下,它仍然返回其默认的fetch - associative数组。

You need to alias the column with AS in order to access it as column 您需要使用AS为列添加别名,以便将其作为列进行访问

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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