PHP Fatal error: Uncaught Error: Call to a member function fetch_assoc() on boolean in error

Hi All

For the life of me I cannot work out why I am getting this error… the error is:

[24-Aug-2022 07:29:34 Australia/Sydney] PHP Fatal error: Uncaught Error: Call to a member function fetch_assoc() on boolean in /home/client/public_html/shop-product.php:705

My code is:

$productID = $_GET['productID'];

$sql = "SELECT uniqueID FROM product_sizes WHERE productID='" . $productID . "'";
$result = $con->query($sql);

$num_rows = 0;

while($row = $result->fetch_assoc()) { $num_rows ++; } mysqli_free_result( $result );

The code connects to the database correctly, it gets the information from the database correctly and returns the results that I am expecting however every time it is run I get the error.

I have tried removing the $productID variable and adding the productID directly and still the error.

What am I missing?

Any help would be great.


Is you table really named product_sizes? The error means your query is failing. Check spelling of table and column names.

Hi… I don’t think it is the table… I have checked… uniqueID is correct, product_sizes is correct and productID is correct… I even copied and pasted from mySQL to be sure?

I am also getting the results I expect?

Try running the query directly on MySQL

I have done that too and it works fine… I have also tried printing the uniquieID in the while statement and it returns the correct information… but I still get the error message?

I also tried adding 111 to each part of the query in case there was a conflict with other code and still get the error?

$productID = $_GET['productID'];

$sql111 = "SELECT uniqueID FROM product_sizes WHERE productID='" . $productID . "'";
$result111 = $con->query($sql111);

$num_rows = 0;

while($row111 = $result111->fetch_assoc()) { $num_rows ++; } mysqli_free_result( $result111 );

Add this to the top of your script before the connection call.


What does it tell you now when you run it?
EDIT: You can always check the error and it will tell you what is wrong.

Thanks for your help with this… I have added that too… if I just add it I get nothing… if I add it as:


while($row = $result->fetch_assoc()) { $num_rows ++; print $error; } mysqli_free_result( $result );

I get 10 1’s - 1111111111

I am expecting 10 results from the query and when I print $num_rows I get 1 to 10 and when I print the uniqueID I get the correct ID’s.

I wonder if there is a conflict with something else on the page?

I didnt tell you to make a variable out of it or add more code.

I know… it did not result in anything when I just added the code… I have now just pulled the code above out of all the code and run it and there is no error in the error log so maybe the code is correct and it is getting an error from elsewhere… is that possible?

Did you add that line BEFORE the connection call?

Either the line where the error is occurring is somewhere else or this code is inside of some loop you are not showing in the post and the final pass through that loop is when the error is occurring.

Why are you querying to get all the rows of matching data just to count how many rows there are? Just use a SELECT COUNT(*) … query and fetch the count value.

Edit: this symptom is typical of code being requested twice, one with data and a second time without. What is the code that’s requesting this page?

Yes… I have found a pile more error messages in my error_log:

[24-Aug-2022 08:38:54 Australia/Sydney] PHP Fatal error: Uncaught mysqli_sql_exception: Table ‘blanktshirtscom_blanktshirts.size’ doesn’t exist in /home/blanktshirtscom/public_html/shop-product.php:700
Stack trace:
#0 /home/blanktshirtscom/public_html/shop-product.php(700): mysqli->query(‘SELECT uniqueID…’)
#1 {main}
thrown in /home/blanktshirtscom/public_html/shop-product.php on line 700

There are a pile of tables in the error that do not exist and and never have and I have never seen?

Thanks for that… it is not in another while loop and I had count originally and also got errors.

Fix the errors in your error log and then go from there.

That is what I am trying to do… the new errors after adding - mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); - makes no sense… it is referencing tables that have never existed anywhere in the account at all?

I have moved the code to the top of the page and still get the error however when I run the same code in a clean file with only that in it I don’t get any errors… is it possible that something else could be causing this error?

The errors are telling you exactly where the non exist tables are being called. Either fix the calling code or add the tables the code is looking for. Until you fix that there is no point in doing anything else.

Now the error_log is not being created… maybe there is an issue with the server as it was referencing about 20 tables that have never existed

Why should the error log be created, you were already looking at it. Did you delete it? If so, you need to restart Apache.

You should be using Prepared Statements instead of concatenating variables into your query like that. Does your $productID contain any single-quotes? Or is it a numeric value, in which case it shouldn’t have quotes around it in the first place. If it was a prepared statement, neither of those things would make any difference.

Why do you have this loop of code?

$num_rows = 0;

while($row = $result->fetch_assoc()) { $num_rows ++; } mysqli_free_result( $result );

If all you want to achieve there is to find out how many rows were returned by the query, why not just use $result->num_rows, though I note there is a distinction between buffered and un-buffered result sets - I don’t use mysqli (I prefer PDO) so I don’t know what that means.

In any case, if your column is called uniqueID, surely you’ll either get zero or one results from the query?