php 警告 mysql_fetch_assoc [英] php warning mysql_fetch_assoc

查看:48
本文介绍了php 警告 mysql_fetch_assoc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
}

推荐答案

一般情况下,mysql_*函数的用法如下:

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
}

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

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.

常见问题:如果您将某些内容(有效查询字符串除外)传递给 mysql_query,则会收到此警告:

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

这篇关于php 警告 mysql_fetch_assoc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆