php警告mysql_fetch_assoc

dea*_*id 1 php mysql

我试图从mysql访问一些信息,但我收到警告:mysql_fetch_assoc():提供的参数不是下面第二行代码的有效MySQL结果资源,任何帮助将不胜感激.

$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;
}
Run Code Online (Sandbox Code Playgroud)

Pis*_*3.0 5

通常,mysql_*函数使用如下:

$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
}
Run Code Online (Sandbox Code Playgroud)

如果您将某些内容传递给不是MySQL结果资源的mysql_fetch_assoc(无论是字符串,对象还是布尔值),该函数会抱怨它不知道如何处理该参数; 这正是你所看到的.

常见问题:如果您将某些内容(有效查询字符串除外)传递给以下内容,则会收到此警告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
Run Code Online (Sandbox Code Playgroud)