tags:

views:

59

answers:

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

+2  A: 

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.

Marc B