Home  >  Article  >  Database  >  Why Does My PHP Code Throw a \"Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource\" Error?

Why Does My PHP Code Throw a \"Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource\" Error?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-30 20:30:30126browse

Why Does My PHP Code Throw a

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:

  • The query execution failed and mysql_query() returned false.
  • The $result variable was overwritten elsewhere in the code.

Solution:

To resolve this error, ensure the following:

  • The query executed successfully by checking the return value of mysql_query().
  • The $result variable is not being modified or overwritten within the loop.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn