Introduction: That Error That Makes You Want to Close the Browser
If you've ever written a PHP query to MySQL and suddenly saw something like this in your browser:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /var/www/site/index.php on line 15...then you know that feeling. Everything seems correct: you connected, wrote the query, but PHP is yelling at you in red. It's especially frustrating when the code works on your local server but not on the hosting. Or vice versa.
This error is one of the most popular on Stack Overflow. Thousands of developers (including us) have banged their heads on the keyboard trying to understand: "Why does parameter 1 expect a resource but receive a boolean?"
In this article, we won't just analyze the cause; we'll go through the entire process: from reproducing the error to fixing it. Along the way, we'll also look at why the old mysql_* functions should be replaced with modern PDO or MySQLi. Let's go!
What the Problem Looks Like in Practice
Let's simulate a situation. You have a users table and want to display all users. The code is written the old way:
$link = mysql_connect('localhost', 'user', 'password');mysql_select_db('test');
$result = mysql_query("SELECT * FROM users");
while ($row = mysql_fetch_array($result)) { echo $row['name'] . '<br>';}It looks like a classic example. But if the users table doesn't exist, or there's a syntax error in the SQL, then mysql_query() will return not a resource, but false. And then mysql_fetch_array() receives a boolean instead of a resource — hence the error.
Most often, the problem lies precisely in an unsuccessful query execution. But let's break down all possible causes in order.
Cause #1: The SQL Query Returned false
This is the most common situation. mysql_query() returns false if the query failed. For example:
- The table doesn't exist
- Incorrect field name
- Syntax error in SQL
- Permission issues
How to check? Simply add a check after the query:
$result = mysql_query("SELECT * FROM non_existent_table");
if (!$result) { die('Query error: ' . mysql_error());}
while ($row = mysql_fetch_array($result)) { // ...}This will immediately show what's wrong. mysql_error() will output the error text, for example: "Table 'test.non_existent_table' doesn't exist".
Cause #2: Lost Connection to the Database
Sometimes the connection drops (e.g., due to a timeout or MySQL restart). Then mysql_query() will also return false. The solution is to always check the connection:
$link = mysql_connect('localhost', 'user', 'password');if (!$link) { die('Could not connect: ' . mysql_error());}But even this isn't a silver bullet. It's better to use functions that can automatically reconnect — for example, MySQLi.
Cause #3: You Are Confusing mysql_* and mysqli_* Functions
Another common case. You might accidentally mix two different libraries. For example, connect using mysqli_connect(), but execute the query using mysql_query(). This won't work: mysql_query() expects a resource from mysql_connect(), not from mysqli_connect().
Here's an example of the confusion:
// Connection via MySQLi$link = mysqli_connect('localhost', 'user', 'password', 'test');
// But query via old mysql_*$result = mysql_query("SELECT * FROM users"); // Error!
while ($row = mysql_fetch_array($result)) { echo $row['name'];}The solution is to stick to one library. Ei