mysql_fetch_array() Error: Parameter 1 Must Be a Resource — How to Fix

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: "Your Logo Isn't Working, and the Database is Crying"

\n\n

If 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.

\n\n

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\n

In 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).

\n\n

This article is written specifically for pythonlib.ru — an educational portal where we teach you to write code without pain. Let's go!

\n\n

Why Does PHP Shout About the Parameter? (Causes)

\n\n

To understand the root of the evil, let's recall how the old mysql_* functions work (yes, the ones deprecated since PHP 5.5).

\n\n

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\n

What's wrong here? Everything! But the main thing: mysql_query() returned false, not a resource. Why?

\n\n
    \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
\n\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.

\n\n

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
\n\n

They all shout the same thing: "expects parameter 1 to be resource".

\n\n

Step-by-Step Fix (for Legacy Code)

\n\n

If 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\n

Step 1. Check the Connection

\n
$link = mysql_connect('localhost', 'user', 'password');\nif (!$link) {\n    die('Connection error: ' . mysql_error());\n}
\n\n

Step 2. Check the Database Selection

\n
if (!mysql_select_db('my_database')) {\n    die('Could not select database: ' . mysql_error());\n}
\n\n

Step 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\n

See the difference? We check every step. If mysql_query() returns false — we die with an error message, rather than passing false further.

\n\n

Why Does This Still Work? (Historical Context)

\n\n

On 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_*.

\n\n

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.

Blogs

Book Recommendations