How to remove mysql_fetch_assoc() error?

How to remove this error??

mysql_fetch_assoc() expects parameter 1 to be resource, boolean

HI…I have tried everything change the query etc but nothing worked…

<?php 
    include 'setup.php';
    $q ="SELECT * FROM Pages WHERE id = 1";
    $r=mysqli_query($conn,$q);
    $page=mysqli_fetch_assoc($r);
?>

this is the code

Hi hariskhan5566 welcome to the forum

The “expects resource, boolean” suggests that $conn was meant to be a database connection (resource / handle) that failed. i.e. returns resource on success, false on failure.

In other words, the problem is with the code in the setup.php file, not necessarily the query.

Please post the setup code - with anything sensitive redacted.

You have an error in your query.

Add the following line to your setup.php permanently and run your code again to see the actual problem. And then fix it.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$page=mysqli_fetch_assoc($r);

This line is correctly coded, however your problem is that when you called mysqli_query() on the line above, something caused the query to fail as @colshrapnel said. When the query fails, instead of returning a result set, it returns false. Hence you’re passing in ‘false’ to mysqli_fetch_assoc(), not a result set (resource), hence the error.

You should check whether your query worked successfully before you start trying to use the results.

1 Like

D’oh!
I’m glad others are more alert than I was. I don’t know how I missed the mysqli_fetch_assoc and thought it was the mysqli_query line. :blush:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.