Call fetch assoc on boolean
suggest changeIf you get an error like this:
Fatal error: Call to a member function fetch_assoc() on boolean in C:\xampp\htdocs\stack\index.php on line 7
Older PHP versions using the removed mysql extension reported variations such as:
mysql_fetch_assoc() expects parameter 1 to be resource, boolean given...
These errors mean that there is something wrong with either your query (this is a PHP/MySQL error), or your referencing. The above error was produced by the following code:
$mysqli = new mysqli("localhost", "root", "");
$query = "SELCT * FROM db"; // notice the errors here
$result = $mysqli->query($query);
$row = $result->fetch_assoc();
With PHP 8, configure MySQLi to throw exceptions so the query failure is reported at its source:
// add this at the start of the script
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
This will then throw an exception with this much more helpful message instead:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELCT * FROM db' at line 1
Passing a value of the wrong type to mysqli_fetch_assoc() is also an error:
$notAResult = true;
mysqli_fetch_assoc($notAResult); // TypeError: argument must be a mysqli_result
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents