简体   繁体   English

MySQL SELECT COUNT返回NULL

[英]MySQL SELECT COUNT returning NULL

I'm trying to get the number of comments on a particular item. 我正在尝试获取特定项目的评论数。

$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 works fine, but $numComments seems to be returning NULL (from var_dump ) and I'm not sure why. $numLikes正常工作,但$numComments似乎返回NULL(从var_dump ),我不确定为什么。 The SQL in phpMyAdmin works fine and returns the number of comments... phpMyAdmin中的SQL可以正常工作并返回注释数...

You probably want change this for your first query 您可能想在第一个查询中更改此设置

... AND r_value=3 OR r_value=2

To

... AND (r_value=3 OR r_value=2)

OR THIS 或这个

... AND r_value IN (2,3)

But for the second query try something simple just for debug 但是对于第二个查询,请尝试一些简单的调试操作

$stmt2 = $conn->prepare("SELECT * FROM comments WHERE c_snippet=10");

then 然后

$stmt2 = $conn->prepare("SELECT count(*) FROM comments WHERE c_snippet=10");

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

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