开发者

Query returned as Boolean?

开发者 https://www.devze.com 2023-01-22 08:47 出处:网络
Can\'t figure out whats wrong with this. $replies_sql = \"SELECT COUNT(*) AS total FROM forum_posts WHERE forum_posts.thread_id = 1\";

Can't figure out whats wrong with this.

$replies_sql = "SELECT COUNT(*) AS total
                  FROM forum_posts
                 WHERE forum_posts.thread_id = 1";
开发者_高级运维

I'm trying to calculate the total replies in a specific thread. I am just testing on thread_id 1 at the moment.

Error:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given


Most likely the query failed for whatever reasons, and returned boolean FALSE, which you then passed on to the fetch_assoc() call. You should restructure your code like this:

$stmt = mysqli_query($replies_sql);
if ($stmt === FALSE) {
    die("MySQL error: " . mysqli_error($stmt));
}
$res = mysqli_fetch_assoc($stmt);

never assume a database query will succeed. There's only one way to succeed, and far too many ways to fail.

0

精彩评论

暂无评论...
验证码 换一张
取 消