我正在尝试获取特定项目的评论数量。
$stmt1 = $conn->prepare("SELECT COUNT(r_value) FROM ratings WHERE r_snippet=? AND r_value=3 OR r_value=2");
$stmt2 = $conn->prepare("SELECT COUNT(c_id) FROM comments WHERE c_snippet=?");
foreach ($snippets as $snippet){
$s_id = $snippet['s_id'];
$s_thumb = $snippet['s_thumb'];
$stmt1->bind_param("i",$s_id);
$stmt1->execute();
$stmt1->bind_result($numLikes);
$stmt1->fetch();
$stmt2->bind_param("i",$s_id);
$stmt2->execute();
$stmt2->bind_result($numComments);
$stmt2->fetch();
?>
** HTML here **
<?php
}
$stmt1->close();
$stmt2->close();
?>
$numLikes
工作正常,但$numComments
似乎返回NULL(来自var_dump
),我不知道为什么。 phpMyAdmin中的SQL工作正常并返回注释数...
答案 0 :(得分:0)
您可能希望在第一次查询时更改此内容
... AND r_value=3 OR r_value=2
到
... AND (r_value=3 OR r_value=2)
或者这个
... AND r_value IN (2,3)
但是对于第二个查询,尝试一些简单的调试
$stmt2 = $conn->prepare("SELECT * FROM comments WHERE c_snippet=10");
然后
$stmt2 = $conn->prepare("SELECT count(*) FROM comments WHERE c_snippet=10");