查询以布尔值返回吗? [英] Query returned as Boolean?

查看:82
本文介绍了查询以布尔值返回吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法弄清楚这是怎么回事.

Can't figure out whats wrong with this.

$replies_sql = "SELECT COUNT(*) AS total
                  FROM forum_posts
                 WHERE forum_posts.thread_id = 1";

我正在尝试计算特定线程中的总回复.我现在仅在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.

错误:

警告:mysqli_fetch_assoc()期望参数1为mysqli_result,给定布尔值

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

推荐答案

由于各种原因,查询很可能失败,并返回布尔FALSE,然后将其传递给fetch_assoc()调用.您应该像这样重组代码:

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.

这篇关于查询以布尔值返回吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆