Call fetch assoc on boolean

suggest change

If 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

Other variations include something along the lines of:

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();

In order to “fix” this error, it is recommended to make mysql throw exceptions instead:

// 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

Another example that would produce a similar error, is where you simply just gave the wrong information to the mysql_fetch_assoc function or similar:

$john = true;
mysqli_fetch_assoc($john, $mysqli); // this makes no sense??

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Common Errors:
* Call fetch assoc on boolean

Table Of Contents
2 Arrays
4 Types
10 Cookies
14 JSON
15 SOAP
17 cURL
19 XML
21 Traits
35 UTF-8
36 URLs
38 PHPDoc
41 Loops
44 Closur
61 Common Errors
72 YAML
77 Cache
78 Streams
81 PDO
82 SQLite3
83 Sockets
87 MongoDB
93 IMAP
94 Redis
95 Imagick
102 APCu
108 PSR