'Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in...' but my query is correct

'Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in...' but my query is correct

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The Silent mysql_fetch_array Warning in PHP 5.3

You are staring at a classic error message: "Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given." Your logic is sound; your SQL is valid. You've even run $result through a var_dump() and it returns resource(3). So why is PHP complaining?

The issue isn't that the query failed at the database engine level; it's that the function you are calling to retrieve rows has already terminated before you reached your while loop.

In the scenario described, the code connects to the database, selects the database, escapes the input, and executes a SELECT * statement. If $result is indeed a resource when inspected immediately after mysql_query(), but becomes a boolean by the time it hits mysql_fetch_array(), there is usually one specific culprit: connection state corruption.

The deprecated mysql_* extension in PHP 5.3 does not always report errors for every operation that fails. A query might return a resource handle even if the underlying connection was subtly compromised or if the server silently rejected the request due to permissions or configuration issues that don't trigger an explicit error string via mysql_error().

However, the most common reason for this specific symptom—where var_dump() shows a resource but the fetch fails immediately—is often related to how the result handle is managed across different connection contexts or if the connection was inadvertently closed between the query execution and the fetch. In some versions of PHP 5.3, particularly when mixing extensions or using certain hosting environments, the internal state of the mysql extension can become inconsistent.

Consider this flow:

  1. mysql_connect() succeeds.
  2. mysql_query() executes and returns a resource.
  3. Something happens. The connection is effectively dead, but the handle isn't cleaned up.
  4. mysql_fetch_array() checks the internal link ID. It sees that the previous operation failed internally (even if no error was thrown), returns false, and PHP triggers the warning because you passed false instead of a valid resource to the fetch function.

The fact that "everything within the while statement is being executed just fine" suggests that the first row might have been fetched successfully, or that the loop logic is handling the false return value gracefully in some iterations but not others. Wait, if the query never throws an error and var_dump shows a resource, the issue might be simpler: you are likely fetching from the wrong variable or the handle was modified.

But there is a more subtle possibility often overlooked in PHP 5.3. If you are running multiple queries on the same connection without clearing the result set (not applicable to SELECT * usually, but relevant for logic), or if the server configuration (my.ini) has specific limits reached, the handle might be invalid internally despite looking valid externally.

The most pragmatic solution in this legacy environment is to explicitly check the return value of mysql_query() before attempting to fetch any rows, even if you don't see an error message. The pattern should look like this:

$query = "SELECT * FROM newuser WHERE token='$token'";
$result = mysql_query($query);

// Always check immediately after query
if (!$result) {
    die("Query failed: " . mysql_error());
}

while ($row = mysql_fetch_array($result)) {
    // Do stuff...
}

If you add the if (!$result) block and still don't get an error message inside that block, it confirms that the connection is silently broken. This often happens when the user permissions on the MySQL server side do not match the expected privileges for the specific database or table, causing a "false positive" success in the PHP layer but a failure in the data retrieval layer.

While you plan to migrate to mysqli or PDO, understanding this behavior is crucial during the transition. The modern extensions handle connection states more strictly and will throw exceptions or return explicit nulls much faster when a resource is invalid. Until then, treat every mysql_query() result as potentially fragile in PHP 5.3.

If you are maintaining legacy code for a specific project where migration isn't immediate, ensure that your .htaccess or php.ini settings aren't interfering with the MySQL extension's internal buffers. However, the most reliable fix remains upgrading the stack. The mysql_* functions were removed in PHP 7.0 and deprecated in 5.5 specifically because of these exact kinds of silent state inconsistencies.

For a robust implementation today, switch to PDO or MySQLi. These drivers allow you to use prepared statements, which eliminate the SQL injection risk inherent in mysql_escape_string() and provide clearer error reporting when a resource is invalid:

$pdo = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$stmt = $pdo->prepare("SELECT * FROM newuser WHERE token = :token");
$stmt->execute(['token' => $_GET['token']]);
$rows = $stmt->fetchAll(); // No warning here if query fails

This approach ensures that if the query fails or returns no results, you get a clean array or an exception without confusing "resource vs boolean" warnings.

Our network