MySQL Fetch Function Error: Invalid Result Resource
Problem:
When using the mysql_fetch_assoc() function in PHP to retrieve data from a MySQL query, you may encounter the following error:
<code class="php">Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource</code>
Cause:
This error typically occurs because the $result variable passed to mysql_fetch_assoc() does not refer to a valid MySQL result resource. This can happen if:
Solution:
To resolve this error, ensure the following:
Here's an example of how to handle the error:
<code class="php">$query = "SELECT UniqueID FROM configuration"; $result = mysql_query($query); if (!$result) { die(mysql_error()); } while ($row = mysql_fetch_assoc($result)) { // Do something with the row }</code>
Additional Note:
As mentioned in the error message itself, this issue can also be caused by duplicate result resource usage. Make sure you're not reusing the same result resource for multiple queries, as this can lead to unexpected results.
The above is the detailed content of Why Does My PHP Code Throw a \'Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource\' Error?. For more information, please follow other related articles on the PHP Chinese website!