gpt4 book ai didi

php - MySQL SELECT COUNT 返回 NULL

转载 作者:行者123 更新时间:2023-11-29 21:56:18 26 4
gpt4 key购买 nike

我正在尝试获取特定项目的评论数量。

$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 工作正常并返回评论数...

最佳答案

您可能希望为您的第一个查询更改此设置

... 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");

关于php - MySQL SELECT COUNT 返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33129606/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com