6

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!

robere2
  • 1,689
  • 2
  • 16
  • 26
  • 2
    According to your dumps, the results returned from the query is one row. Thus, when you do `var_dump($mysqli_query->fetch_assoc())` you actually fetch that one row, so when you then try to do `while($row = $mysqli_query->fetch_assoc()) {}` afterwards, there are no more rows to fetch. As for the error-message you get, if `$mysqli_query` is a boolean, it means the query failed, and you need to figure out what happened - using [`mysqli_error`](http://php.net/manual/en/mysqli.error.php) will likely tell you what went wrong. – Qirel Nov 20 '16 at 04:55
  • There is no error in `$mysqli->error` or `mysqli_error($mysqli)`. As for that `var_dump` thing I was running into, that makes sense. Thanks. :) – robere2 Nov 20 '16 at 05:05
  • While everything looks fine, if it doesn't work, there are obviously some errors. I suggest you do some basic debugging, like `echo $mysqli_cmd;` to verify the syntax of the query. By the way, `$mysqli->error` is practically the same as `mysqli_error($mysqli)`, so doesn't matter which one you choose (but if you use OOP, stay with OOP). – Qirel Nov 20 '16 at 05:10
  • echoing `$mysqli_cmd` looks fine to me, outputting: SELECT * FROM `hosting_signups` WHERE `date`='2016-11-19' Excuse the formatting, but I don't know how to include a backtick without it messing up due to the ones present in the MYSQL command. I am aware that `mysqli_error` is the same as `$mysqli->error`, although you suggested the former while I use the latter so I was just covering all grounds. :) Sorry, I've honestly tried all the debugging things I can think of (which is not many). Any suggestions would be appreciated. – robere2 Nov 20 '16 at 05:18
  • FYI, you can output backticks by escaping it `\\``, but if the query is fine, it should work. But yeah, the query looks fine, and `$mysqli->error` didn't produce anything, then it's weird that you get that error "*Call to a member function fetch_assoc() on boolean*". You sure it's still producing it? – Qirel Nov 20 '16 at 05:26
  • And if you run `SELECT * FROM \`hosting_signups\` WHERE \`date\`='2016-11-19'` in phpMyAdmin, does it work? – Qirel Nov 20 '16 at 05:32
  • Running in phpMyAdmin does work still, yes, displaying one row. I've edited my main post to contain more information. The error does still persist. – robere2 Nov 20 '16 at 05:37
  • I just tried taking **only** the code producing the error and running that in a separate file, and that seems to work fine, oddly.. I'm gonna try going through the file with the other code now and removing bit by bit to see if it works then.. – robere2 Nov 20 '16 at 05:46
  • From what my tired eyes can see, the code you provided, it should indeed work. Stupid question: are you looking at the right file and at the right lines? :p – Qirel Nov 20 '16 at 05:47
  • Yes and yes. Unless I've got tired eyes, too, and they're repeatedly failing me. I'll try what I mentioned in the comment above now quickly and get back to you with the results. – robere2 Nov 20 '16 at 05:49

3 Answers3

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;
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
  • We already know the number of rows, it's in the first `var_dump()` :-) `["num_rows"] => int(1)` – Qirel Nov 20 '16 at 04:58
  • 1
    added `or die($mysqli->error);` and my script ignored it, proceeding as normal. There is no error there. – robere2 Nov 20 '16 at 05:06
1

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.

robere2
  • 1,689
  • 2
  • 16
  • 26
0

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

Shadow
  • 1