1

I am trying to access some information from mysql, but am getting the warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource for the second line of code below, any help would be much appreciated.

$musicfiles=getmusicfiles($records['m_id']);
$mus=mysql_fetch_assoc($musicfiles);
for($j=0;$j<2;$j++)
{
 if(file_exists($mus['musicpath']))
 {
  echo '<a href="'.$mus['musicpath'].'">'.$mus['musicname'].'</a>';       
 }
 else
 {
  echo 'Hello world';     
 }
}

function getmusicfiles($m_id)
{
$music="select * from music WHERE itemid=".$s_id;
$result=getQuery($music,$l);
return $result;
}
6
  • What's the definition of getmusicfiles()? Commented Dec 14, 2009 at 15:16
  • And what's the definition of getQuery()? Is there a call to mysql_query in there, somewhere? If not, you will get the warning, as it should be. Commented Dec 14, 2009 at 15:31
  • yeah there is, and it works fine since I used it quite a bit in my site Commented Dec 14, 2009 at 15:35
  • @death the kid: If it worked fine, you wouldn't be asking this question ;) Can we see it? Commented Dec 14, 2009 at 15:38
  • 4
    Note that your parameter ($m_id) is not the same as the value you're trying to append to your SQL querty ($s_id) - perhaps this is your problem? Commented Dec 14, 2009 at 15:41

6 Answers 6

5

Generally, the mysql_* functions are used as follows:

$id = 1234;
$query = 'SELECT name, genre FROM sometable WHERE id=' . $id;
// $query is a string with the MySQL query
$resource = mysql_query($query);
// $resource is a *MySQL result resource* - a mere link to the result set
while ($row = mysql_fetch_assoc($resource)) { 
    // $row is an associative array from the result set
    print_r($row);
    // do something with $row
}

If you pass something to mysql_fetch_assoc that is not a MySQL result resource (whether it's a string, an object, or a boolean), the function will complain that it doesn't know what to do with the parameter; which is exactly what you are seeing.

A common gotcha: you get this warning if you pass something (other than a valid query string) to mysql_query:

$id = null;
$query = 'SELECT name, genre FROM sometable WHERE id=' . $id;
$res = mysql_query($query); 
// $res === FALSE because the query was invalid
// ( "SELECT name, genre FROM sometable WHERE id=" is not a valid query )
mysql_fetch_assoc($res); 
// Warning: don't know what to do with FALSE, as it's not a MySQL result resource
Sign up to request clarification or add additional context in comments.

Comments

1

Without seeing the code of getmusicfiles there's not a lot we can really help you with. You should be returning a valid mysql resource in that function.

Comments

1

As others have noted, you need to return a valid mysql resource into the mysql_fetch_assoc function to retrieve the next row. For example:

$sql = "select * from table";

$resultSet = mysql_query($sql) or die("Couldn't query the database.");
echo "Num Rows: " . mysql_num_rows($resultSet);

while ($resultRowArr = mysql_fetch_assoc($resultSet)) {
    ...
}

Comments

0

I think you need to specify what the function getQuery()

$result=getQuery($music,$l);

does

Comments

0

It depends on what exactly getmusicfiles() does. It must return a result of mysql_query() function call, then it will be a "valid MySQL result".

And you most probably wanted to put the line $mus=mysql_fetch_assoc($musicfiles) inside of the for cycle to fetch several rows one after another.

Comments

0
function getmusicfiles($m_id) {
$music="select * from music WHERE itemid=".$s_id;

$m_id != $s_id ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.