Introduction: "Your Logo Isn't Working, and the Database is Crying"
\n\nIf you've ever written a PHP script that works with MySQL, you've likely encountered this frightening phrase:
\nWarning: mysql_fetch_array() expects parameter 1 to be resource, boolean given.
Let's agree right away: this is not the end of the world. It's just a signal from PHP: "Hey, I can't fetch data from what you gave me!".
\n\nIn this tutorial, we'll break down why the mysql_fetch_array() expects parameter 1 to be resource error occurs, how to fix it, and most importantly — how to stop seeing it once and for all (spoiler: switch to PDO or MySQLi).
This article is written specifically for pythonlib.ru — an educational portal where we teach you to write code without pain. Let's go!
\n\nWhy Does PHP Shout About the Parameter? (Causes)
\n\nTo understand the root of the evil, let's recall how the old mysql_* functions work (yes, the ones deprecated since PHP 5.5).
A typical beginner's code looks like this:
\n// Connection\n$link = mysql_connect('localhost', 'user', 'password');\nmysql_select_db('test');\n\n// Query (with an error)\n$result = mysql_query("SELECT * FROM users WHERE id = '1");\n\n// Attempt to read\nwhile($row = mysql_fetch_array($result)) { // Error!\n echo $row['name'];\n}\n\nWhat's wrong here? Everything! But the main thing: mysql_query() returned false, not a resource. Why?
- \n
- Syntax error in SQL — the quote is not closed. \n
- No database connection —
mysql_connect()returned false. \n - Database not selected —
mysql_select_db()didn't work. \n - Table does not exist. \n
When mysql_query() fails, it returns false (a boolean type), not a resource. You then try to pass false to mysql_fetch_array(), which expects a resource. Conflict! Hence the error.
Which Functions Are Affected?
\n- \n
mysql_fetch_array()\n mysql_fetch_assoc()\n mysql_fetch_row()\n mysql_num_rows()\n mysql_data_seek()\n
They all shout the same thing: "expects parameter 1 to be resource".
\n\nStep-by-Step Fix (for Legacy Code)
\n\nIf you have a legacy project and can't update PHP yet (though I highly recommend it), here's how to catch and fix the error.
\n\nStep 1. Check the Connection
\n$link = mysql_connect('localhost', 'user', 'password');\nif (!$link) {\n die('Connection error: ' . mysql_error());\n}\n\nStep 2. Check the Database Selection
\nif (!mysql_select_db('my_database')) {\n die('Could not select database: ' . mysql_error());\n}\n\nStep 3. Make a Query with Validation
\n$sql = "SELECT * FROM users WHERE id = 1";\n$result = mysql_query($sql);\n\nif (!$result) {\n die('Query error: ' . mysql_error());\n}\n\n// Now you can safely use it\nwhile($row = mysql_fetch_assoc($result)) {\n echo $row['name'];\n}\n\nSee the difference? We check every step. If mysql_query() returns false — we die with an error message, rather than passing false further.
Why Does This Still Work? (Historical Context)
\n\nOn Stack Overflow, this question has gathered thousands of views. And it's no surprise: many hosting providers still run PHP 5.x, and plenty of tutorials (even on YouTube) teach using mysql_*.
But remember once and for all: the mysql extension is deprecated and removed from PHP 7.0. If you are writing new code — use mysqli or PDO.