“Call to a member function fetch_assoc() on boolean” [duplicate]

。_饼干妹妹 提交于 2021-01-27 07:38:24

问题


I've seen a bunch of questions about this error, but none of them seemed to have an answer that solved my problem... Sorry if I missed one.

My script keeps giving me an error saying

Call to a member function fetch_assoc() on boolean

but I don't see how this is the case.

Both $mysqli_query and $mysqli_query->fetch_assoc() are objects. Those respectively being:

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

and

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}}}"
}

These also produce the same error:

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

When the following is run in PhpMyAdmin, it works fine:

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

Does anyone know what I'm doing wrong? Here is the relevant code:

$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()) {}

EDIT: I've modified my code a bit, with some more debugging, to be:

$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()) {}

The output is:

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

EDIT 2: Was a stupid mistake on my end.. I was accidentally changing the value of $mysqli_query in the while loop instead of the query variable for another query I was doing inside of the loop. Problem solved!


回答1:


Ok . seems like you are getting an error from your query . so to check that add these codes to your existing query

or die($mysqli->error);

So you can see if you have any errors .

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

also try to echo the returned number of rows .

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



回答2:


Was a stupid mistake on my end.. I was accidentally changing the value of $mysqli_query in the while loop instead of the query variable for another query I was doing inside of the loop.




回答3:


For me the error was the table name; It was 'user' instead of 'User' which I didn't expect because even with the upper case it worked on my local wamp, the error began when I uploaded my project on LWS



来源:https://stackoverflow.com/questions/40700799/call-to-a-member-function-fetch-assoc-on-boolean

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!