gpt4 book ai didi

PHP MySQL SELECT 语句返回 null

转载 作者:行者123 更新时间:2023-11-29 10:11:35 27 4
gpt4 key购买 nike

我想从MySQL中选择数据带有一个 where 条件。

我确信条件为 false,这就是为什么它不应该返回任何数据或任何行,但它返回 null。我用$stmt2->num_rows > 0检查过这个条件是正确的。

如何防止这种情况发生?

我的代码应该如何工作:这个条件第一次应该为假,在我插入一些数据后为真。

我认为这是给 SUM()在 select 语句中使用。 我该怎么做?

$q2="SELECT SUM(liked), SUM(disliked) FROM like_dislike WHERE post_id=?";
$stmt2 = $conn->prepare($q2);
$stmt2->bind_param('i', $post_id);
$stmt2->execute();
$stmt2->store_result();
$stmt2->bind_result($like_status, $dislike_status);
if ($stmt2->num_rows > 0 ) {
$stmt2->fetch();
}
else {
$like_status =0;
$dislike_status =0;
}

我的 select 语句结果在 phpmyadmin:

select statement null

最佳答案

根据documentation :

SUM([DISTINCT] expr)

Returns the sum of expr. If the return set has no rows, SUM() returns NULL. The DISTINCT keyword can be used to sum only the distinct values of expr.

If there are no matching rows, SUM() returns NULL.

因此,如果没有数据,结果将始终为 NULL。

关于PHP MySQL SELECT 语句返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50873128/

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