mysql_fetch_array/mysql_fetch_assoc/mysql_fetch_row/mysql_num_rows etc... expects parameter 1 to be resource
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Understanding MySQL Functions and the "Expects Parameter 1 to be Resource" Error
When working with MySQL in PHP, the mysql_fetch_array(), mysql_fetch_assoc(), mysql_fetch_row(), and mysql_num_rows() functions are commonly used for fetching data from result sets. However, developers often encounter the error "expects parameter 1 to be resource" when using these legacy functions. Understanding why this error occurs and how to resolve it is crucial for efficient database handling in PHP.
The Functions Explained
Here's a brief explanation of each function and how they are typically used:
- mysql_fetch_array(): This function fetches a result row as an associative array, a numeric array, or both. The syntax is as follows:
The function returns the next row of the result set or FALSE if there are no more rows.
- mysql_fetch_assoc(): Similar to
mysql_fetch_array(), this function only returns an associative array where the keys are the column names.
- mysql_fetch_row(): This function returns a numerically indexed array, corresponding to the fetched row.
- mysql_num_rows(): This function will return the number of rows in a result set.
Each of these functions expects a valid resource, specifically a result resource generated by a mysql_query() call.
Understanding the "Expects Parameter 1 to be Resource" Error
Why It Occurs
The error "expects parameter 1 to be resource" occurs when the input parameter is not a valid resource. This typically happens when:
- The
mysql_query()function fails to execute the SQL query properly, returningFALSEinstead of a resource. - The variable assigned to store the result of the query is incorrectly used, potentially being used before assignment or as the result of a failed query.
Example of a Problematic Code
In this example, if the table non_existent_table does not exist, mysql_query() will return FALSE, and mysql_fetch_assoc() will trigger the error because it expects a valid resource.
Solutions
- Error Handling and Validation: Always check if the query execution was successful before proceeding.
- Migrating to MySQLi or PDO: The
mysql_*functions are deprecated in PHP 5.5.0 and removed in PHP 7.0.0. Consider migrating to MySQLi or PDO for better security and performance. - Ensure the Resource is Valid: Before using functions that require a resource, validate the result of
mysql_query().
Migrating from mysql_* Functions
To modernize the code, here’s how you might use MySQLi:
Summary Table
| Function | Type of Result | Return Type | Error Potential Reason |
mysql_fetch_array() | Array (Assoc + Num) | Array or FALSE | Non-resource parameter |
mysql_fetch_assoc() | Assoc Array | Array or FALSE | Non-resource parameter |
mysql_fetch_row() | Numeric Array | Array or FALSE | Non-resource parameter |
mysql_num_rows() | Number of Rows | Integer or FALSE | Non-resource parameter |
Conclusion
The functions mysql_fetch_array(), mysql_fetch_assoc(), mysql_fetch_row(), and mysql_num_rows() are now obsolete, but understanding how their parameters work is still valuable. An error saying "expects parameter 1 to be resource" points towards a misuse of result handling. Migrating to MySQLi or PDO is recommended for contemporary PHP development, offering improved functionality and security.

