-3
如何返回带有PDO的的计数行数?PHP - 返回计数(*)结果
这是我的代码:SELECT count(*) FROM users_notifications WHERE userid=0 OR userid=:userid
如何返回带有PDO的的计数行数?PHP - 返回计数(*)结果
这是我的代码:SELECT count(*) FROM users_notifications WHERE userid=0 OR userid=:userid
的count
SQL函数是一个聚合函数:
$n=$dbh->prepare("SELECT count(*) FROM users_notifications WHERE userid=0 OR userid=:userid");
$n->bindParam(":userid",$userdata['id']);
$n->execute();
$notifications = count($n);
当我运行在数据库中查询这只是虽然有结果返回1。返回一行,其行数匹配where
子句。
PHP的count
返回结果集中的行数,如前一段所述,它应该为1。相反,您应该检索列的值。例如:
$n=$dbh->prepare("SELECT count(*) as c FROM users_notifications WHERE userid=0 OR userid=:userid");
$n->bindParam(":userid",$userdata['id']);
$n->execute();
$result = $n->fetch(PDO::FETCH_ASSOC);
$notifications = $result['c'];
看看这个问题:http://stackoverflow.com/questions/883365/row-count-with-pdo – Bono 2014-09-13 10:13:51