Home >Database >Mysql Tutorial >Why am I getting the 'Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result' error in my PHP script?
Error: "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result"
When attempting to execute a PHP script, you encounter the following error message:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result [duplicate]
This error occurs when the mysql_fetch_array() function is provided with an invalid MySQL result. To resolve the issue, let's examine the underlying cause:
Invalid MySQL Result
The mysql_fetch_array() function requires a valid MySQL result object as its argument. This object is returned by the mysql_query() function after executing a query. If the query is invalid or fails, the mysql_query() function will return a false value instead of a valid result object.
Checking for Errors
To determine if the query is the source of the issue, you should check the mysql_error() function after executing the query. This function will provide details on any errors that occurred during the query execution.
Example Code:
$result = $connector->query('SELECT title,content FROM staff_vacancies ORDER BY ordering LIMIT 0,100'); if (!$result) { die('Invalid query: ' . mysql_error()); }
Modified query() Function
To improve error handling and provide a more detailed error message, you can modify the query() function in your DbConnector class as follows:
function query($query) { $this->theQuery = $query; $queryId = mysql_query($query, $this->link); if (!$queryId) { throw new Exception(mysql_error() . ". Query was:\n\n" . $query . "\n\nError number: " . mysql_errno()); } return $queryId; }
This modified function will throw an exception with a detailed error message, including the error number and the query that failed. By handling this exception, you can provide a more user-friendly error message to help identify and resolve the issue.
The above is the detailed content of Why am I getting the 'Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result' error in my PHP script?. For more information, please follow other related articles on the PHP Chinese website!