Query returned as Boolean?
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.
精彩评论