The mysql_fetch_array() function in PHP is usually used to convert the query result set into a PHP array. However, when using the mysql_fetch_array() function, you may encounter the following error:
"Warning: mysql_fetch_array() expects parameter X to be resource, boolean given"
where X can be 1 or 2, this is due to incorrect parameter types being passed when the function is called. In this article, we will discuss the causes of this error and how to fix it.
Error reason
This error indicates that the parameter type passed in the function call is incorrect. In the mysql_fetch_array() function, the first parameter must be the identifier of a query result set, returned by the mysql_query() function.
If what is passed is not a result set, but a Boolean value, the above error will occur. Usually, this error is caused by the following reasons:
1. The query is not executed
Before calling the mysql_fetch_array() function, you must first use the mysql_query() function to issue a query request, and Get the result set. If the query was not executed, the mysql_query() function will return false as a boolean value, so when passed to the mysql_fetch_array() function, an error will occur.
2. Query failure
If the query request fails to execute, a false will be returned instead of the result set identifier. Likewise, when trying to pass false to the mysql_fetch_array() function, the error occurs.
3. Use wrong syntax
When using wrong SQL syntax, the query will not be executed and a false value will be returned. Likewise, when trying to pass false to the mysql_fetch_array() function, the error occurs.
Solution
1. Check whether the query has been successfully executed
Before calling the mysql_fetch_array() function, please ensure that a query has been successfully executed using the mysql_query() function. And the identifier of the result set has been obtained. If the query is not executed successfully, the mysql_query() function will return false. Therefore, before calling the mysql_fetch_array() function, check whether the query has been successfully executed as follows:
$result = mysql_query("SELECT * FROM table_name");
if($ result !== false) {
//Query successful
$row = mysql_fetch_array($result);
//Use the result set
}
else {
//Query failure
}
2. Processing query failure
If the query execution is unsuccessful, you must Check for errors and resolve issues. The following code snippet illustrates how to check for query failure and output an error message:
$result = mysql_query("SELECT * FROM table_name");
if($result === false) {
//Query failed
echo "MySQL error:" . mysql_error();
}
else {
//Successfully obtained Result set
$row = mysql_fetch_array($result);
}
3. Use correct syntax
Ensure that the SQL syntax is correct to avoid query failure . If in doubt, please check the MySQL documentation or refer to the MySQL book.
Summary
When using the mysql_fetch_array() function, if the error message "Warning: mysql_fetch_array() expects parameter X to be resource, boolean given" appears, please check whether the code segment has been successful. Execute the query and see if the query fails to execute or has incorrect syntax. If you follow the suggestions above, you should be able to successfully resolve the error.
The above is the detailed content of PHP Warning: mysql_fetch_array() expects parameter solution. For more information, please follow other related articles on the PHP Chinese website!