Home > Database > Mysql Tutorial > body text

Why Am I Getting the \'mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource\' Warning?

Barbara Streisand
Release: 2024-10-28 05:55:02
Original
239 people have browsed it

  Why Am I Getting the

"php warning mysql_fetch_assoc" Mystified?

When attempting to retrieve data from a MySQL database, it's not uncommon to encounter the disconcerting "mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource" warning. This problem arises when the argument passed to the mysql_fetch_assoc() function is not a valid result set pointer.

Understanding the Cause

The mysql_fetch_assoc() function is designed to extract an associative array from a MySQL result set. The result set, in turn, is obtained by executing a query with mysql_query(). Therefore, the argument passed to mysql_fetch_assoc() must be a valid result set pointer, which is typically returned by mysql_query().

Diving into the Code

In your provided code snippet, the problem lies in the second line:

<code class="php">$mus = mysql_fetch_assoc($musicfiles);</code>
Copy after login

The variable $musicfiles does not contain a MySQL result set pointer. Instead, it contains the result of a function call to getmusicfiles(). This function appears to be executing a query and returning the result (presumably using mysql_query()), but the return value is not being stored in a variable.

The Correct Approach

To resolve this issue, you need to assign the result of the mysql_query() call to a variable. Here's how you can rewrite the relevant portion of your code:

<code class="php">$result = getmusicfiles($records['m_id']);
$mus = mysql_fetch_assoc($result);</code>
Copy after login

By assigning the result of mysql_query() to $result, you ensure that mysql_fetch_assoc() has a valid result set pointer to work with.

The above is the detailed content of Why Am I Getting the \'mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource\' Warning?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template