php – “在布尔值上调用成员函数fetch_assoc()”

我已经看到了一些关于这个错误的问题,但是他们似乎都找不到解决我问题的答案…抱歉,如果我错过了一个.

我的脚本一直给我一个错误说

Call to a member function fetch_assoc() on boolean

但我不明白这是怎么回事.

$mysqli_query和$mysqli_query-> fetch_assoc()都是对象.分别是:

object(mysqli_result)#4 (5) {
["current_field"]=>
  int(0)
["field_count"]=>
  int(2)
["lengths"]=>
  NULL
["num_rows"]=>
  int(1)
["type"]=>
  int(0)
}

array(2) {
["date"]=>
  string(10) "2016-11-19"
["roles"]=>
  string(241) "{"eu":{"host":{"max":2,"0":"U0SEMUG8L"},"chat":{"max":1,"0":"U0SEMUG8L"},"bg":{"max":2,"0":"U0SEMUG8L"}},"us":{"host":{"max":2,"0":"U0SEMUG8L","1":"U0SEMUG8L","2":"U0SEMUG8L"},"chat":{"max":1,"0":"U0SEMUG8L","1":"U0SEMUG8L"},"bg":{"max":2}}}"
}

这些也会产生同样的错误:

SELECT * FROM `hosting_signups`
SELECT * FROM `hosting_signups` WHERE 1

在PhpMyAdmin中运行以下命令时,它可以正常工作:

SELECT * FROM `hosting_signups` WHERE `date`='2016-11-19'

有谁知道我做错了什么?这是相关代码:

$mysqli_cmd = "SELECT * FROM `hosting_signups` WHERE `date`='" . $next_karaoke->format("Y-m-d") . "'";
$mysqli_query = $mysqli->query($mysqli_cmd);
//var_dump($mysqli_query->fetch_assoc()); // Oddly, when uncommented this terminates the 
                                          // whole while loop below, and the error is not 
                                          // produced but my code inside does not run. I'm 
                                          // not sure if this is at all related to my problem.

while($row = $mysqli_query->fetch_assoc()) {}

编辑:
我已经修改了我的代码,有一些更多的调试,是:

$mysqli_cmd = "SELECT * FROM `hosting_signups` WHERE `date`='" . $next_karaoke->format("Y-m-d") . "'";
$mysqli_query = $mysqli->query($mysqli_cmd);

var_dump($mysqli_cmd);
var_dump($mysqli->error);

while($row = $mysqli_query->fetch_assoc()) {}

输出是:

string(57) "SELECT * FROM `hosting_signups` WHERE `date`='2016-11-19'"
string(0) ""
Fatal error:  Call to a member function fetch_assoc() on boolean in 
/home2/bugfroggy/public_html/hosting_signup.php on line 63

编辑2:
在我的结尾是一个愚蠢的错误..我不小心改变了while循环中$mysqli_query的值,而不是我在循环内部进行的另一个查询的查询变量.问题解决了!

最佳答案 好 .好像您从查询中收到错误.所以要检查是否将这些代码添加到现有查询中

or die($mysqli->error);

所以你可以看看你是否有任何错误.

$mysqli_query = $mysqli->query($mysqli_cmd) or die($mysqli->error);

也尝试回显返回的行数.

echo "number of rows: " . $mysqli_query->num_rows;
点赞