Decoding the mysql_fetch_assoc() Resource Error
The error message Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given is a classic indicator of a specific architectural failure in PHP applications using the legacy MySQL extension. It is not merely a case of missing data; it is a fundamental mismatch between your query execution logic and your assumption that a database connection was successfully established and maintained throughout the script's lifecycle.
When you encounter this warning, the underlying reality is that $result is a boolean false, not a result resource. The mysql_fetch_assoc() function strictly requires an active result resource to iterate over rows. If the previous query failed silently or if the connection was lost between the query execution and the fetch attempt, the function receives false and triggers this warning immediately.
A common misconception arises when developers try to handle missing records by checking the row count before fetching data. In your case, attempting to use mysql_num_rows() alongside mysql_fetch_assoc() on a failed query yields the same error type. This is because both functions expect a valid resource as their first argument. If the SQL statement syntax is incorrect, the table doesn't exist, or the credentials are invalid, the underlying mysql_query() function returns false. Passing that false value to either mysql_num_rows() or mysql_fetch_assoc() results in the exact same runtime warning.
The root cause is almost invariably a lack of explicit error handling for the query execution itself. The legacy mysql_* extension does not throw exceptions by default; it returns false on failure and sets the last error message in the global $errno and $err variables (though these are often ignored in modern codebases). To resolve this without suppressing errors with error_reporting(0), you must validate the result of every query immediately after execution.
Here is the correct pattern for handling legacy MySQL queries:
$result = mysql_query($sql);
if (!$result) {
// Handle the specific SQL error that occurred
die('Query failed: ' . mysql_error());
}
// Now it is safe to use fetch functions
while ($row = mysql_fetch_assoc($result)) {
// Process data
}
By checking $result immediately after mysql_query(), you prevent the propagation of a boolean value into subsequent fetch operations. If the query fails, you address the failure at that specific point rather than letting it crash later during iteration or row counting.
While this approach fixes the immediate error, relying on the mysql_* functions is technically unsound for any new development or maintenance work. These functions were deprecated in PHP 5.5 and removed entirely in PHP 7.0 because they lack support for prepared statements, making them highly vulnerable to SQL injection attacks. The modern standard is to migrate to mysqli or PDO.
Switching to mysqli provides a much cleaner interface for handling these scenarios. For instance, using mysqli_query() with the default return behavior still returns boolean, but the API encourages checking the result set explicitly. If you are using prepared statements (which you should be), the syntax shifts slightly:
$stmt = $conn->prepare("SELECT * FROM users WHERE id = ?");
$stmt->bind_param("i", $userId);
$stmt->execute();
$result = $stmt->get_result(); // Returns mysqli_result, not boolean false on SQL syntax errors
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
// Safe to fetch here
}
} else {
// Handle no results found gracefully
}
In this modern implementation, the distinction between a "no rows found" scenario and a "query failed" scenario is clearer. The get_result() method will throw an exception if the SQL syntax is invalid or if the connection drops, rather than returning a confusing boolean false. This allows you to catch specific exceptions using try-catch blocks, providing a much more robust error reporting mechanism than the legacy extension.
Ultimately, preventing this warning requires acknowledging that database queries can fail for reasons beyond just missing data. Whether it is a syntax error, permission issue, or connection timeout, validating the query result immediately after execution is the only reliable way to ensure your application handles these states gracefully without resorting to disabling error reporting.