"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>
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>
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!