-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