Home > Article > Backend Development > PHP Warning: mysql_fetch_assoc() expects parameter 1 to be resource solution
In PHP development, MySQL database is often used. But sometimes the following error message appears when using mysql_fetch_assoc(): PHP Warning: mysql_fetch_assoc() expects parameter 1 to be resource.
This error is very common, but it may cause serious problems for beginners. Confused because it's not quite clear what causes this error and how to fix it.
This article will introduce in detail the solution to PHP Warning: mysql_fetch_assoc() expects parameter 1 to be resource.
1. Cause of error
Let’s first explain why this error occurs. This error is caused by incorrect parameters of the mysql_fetch_assoc() function.
The mysql_fetch_assoc() function is to take out the data in the result set and save it into an associative array. But if the parameters of the function are incorrect, for example, the parameter is not a valid link resource, this error will be thrown.
2. Solution
The way to solve this error is actually very simple. You only need to check whether the return value of the mysql_query() function is a valid link resource.
The following is a simple sample code:
$con = mysql_connect("localhost","username","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $con); $result = mysql_query("SELECT * FROM table"); if (!$result) { die('Invalid query: ' . mysql_error()); } while($row = mysql_fetch_assoc($result)) { echo $row['column1'] . " " . $row['column2']; } mysql_close($con);
In the above code, first use the mysql_connect() function to connect to the database, and if the connection is successful, select the database. Then use the mysql_query() function to execute the query statement and return the result set.
Then use an if statement to check whether the return value of mysql_query() is false. If it is false, it means that the query execution failed, you can print out the error message and exit the program.
If the query is executed successfully, use the mysql_fetch_assoc() function to fetch the data in the result set and store it in the associative array, and finally close the mysql connection.
3. Other precautions
In addition to checking the return value of mysql_query(), there are some other precautions.
The mysql extension has been officially deprecated. Although it can still be used, it may be completely eliminated in future PHP versions. Delete, so it is recommended to use mysqli or PDO extension to operate the MySQL database.
When there is an error in the query statement, there may also be problems with the results returned by the mysql_query() function, causing the mysql_fetch_assoc() function to be incorrect. Work. Therefore, you must pay attention to the writing format of SQL statements.
If the mysql_query() function does not query any data when executing the query statement, it will also return a false value, which will trigger the mysql_fetch_assoc() function error. At this time, you can check whether the database user has read permissions in the connected database and other issues.
4. Summary
The above is the solution to PHP Warning: mysql_fetch_assoc() expects parameter 1 to be resource. Remember, only incorrect parameter passing can cause the function to not work properly, so checking whether the return value of the mysql_query() function is a valid link resource is the key to solving this problem. At the same time, it is also very important to use mysqli or PDO extensions and pay attention to the SQL statement writing format and database permissions.
The above is the detailed content of PHP Warning: mysql_fetch_assoc() expects parameter 1 to be resource solution. For more information, please follow other related articles on the PHP Chinese website!