mysql_fetch_array()期望参数1是资源错误问题

时间:2012-05-14 19:51:49

标签: php mysql sql

  

可能重复:
  Warning: mysql_fetch_* expects parameter 1 to be resource, boolean given error

我正在尝试回显一个表的内容,但我在这行代码中遇到错误:

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending 
$result=mysql_query($sql);

while($rows=mysql_fetch_array($result)){ // Start looping table row 

2 个答案:

答案 0 :(得分:3)

这意味着您的查询出错并导致其失败。使用mysql_error()找出原因。

仅供参考,you should stop using mysql_*

答案 1 :(得分:0)

在开始循环之前添加if条件以检查$ result。 避免不必要的双引号。

mysql_connect($host, $username, $password)or die("cannot connect"); 
mysql_select_db($db_name)or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
// OREDER BY id DESC is order result by descending 
$result=mysql_query($sql);

if($result)
{
  while($rows=mysql_fetch_array($result)){ 
       // Start looping table row  
  }//while end
}
else
{
   echo "ERROR:".mysql_errno();
}