mysql执行中出现php错误


php error in mysql execution

好的,我有这个代码:

//this is the hi.php//
<?php
//highlight items//
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("juliver", $con);
$result = mysql_query("SELECT * FROM hi WHERE pp='2'");
$hi = "";
while($row = mysql_fetch_array($result)) //<--- this is the line 13//
  {
  $hi .= "<div id='hicontainer'><a id='download_now' class='imgx' href='#?w=700' rel='popup'><img src='".$row['name']."' />";
  $hi .= "<p>".$row['title']."</p>";
  $hi .= "<a href='#?w=700' id='".$row['id']."' rel='popup' class='imgx'>View full</a>    </div>";
  }
//Lots of lots of code here I just specified those code which in error.//
mysql_close($con);

?>

这就是我要显示输出的地方。

<td>
<!--highlight items-->
<div id="tbtitle">
<img src="Images/galleryicon.png"/><p>Highlight items</p>
</div>
<div id="tblgal">
<? echo $hi; ?>
</div>
</td>

但我收到一个错误:"警告:mysql_fetch_array()要求参数1是resource,在第13行的C:''examplep''htdocs''madeinusa''hi.php中给出布尔值。"

拜托,我受够了。提前谢谢。

$result有一个布尔值,可能是因为查询失败。

在尝试使用之前,您应该验证结果

查询后,您可以执行以下操作来查看查询失败的原因:

if(!$result)
  exit(mysql_error());

或者,更简单地说,您可以使用

$result = mysql_query("SELECT * FROM hi WHERE pp='2'") or die(mysql_error());

首先,您应该在代码中使用正确的异常处理。线路之后

$result = mysql_query("SELECT * FROM hi WHERE pp='2'");

您应该检查它是否返回资源id或中的任何错误

if(!$result){
    // if you use try-catch, then use
   throw new Exception(mysql_error());
   // Or you can use
   die(mysql_error());
}else{
     while($row = mysql_fetch_array($result)) //<--- this is the line 13//
     {
        $hi .= "<div id='hicontainer'><a id='download_now' class='imgx' href='#?w=700'     rel='popup'><img src='".$row['name']."' />";
        $hi .= "<p>".$row['title']."</p>";
        $hi .= "<a href='#?w=700' id='".$row['id']."' rel='popup' class='imgx'>View    full</a>    </div>";
     }
}    

检查以下内容:

mysql_connect("localhost","username","password");

您的查询"SELECT * FROM hi WHERE pp='2'"中可能有错误
尝试直接在mysql中执行该查询,看看会发生什么。确保表名和列拼写正确。

此外,请尝试用反勾字符引用表名和列,以避免意外使用MySQL保留字。

"SELECT*FROM hi WHERE pp='2'"查询可能返回false,这意味着不存在完全填充查询的记录。尝试echo mysql_num_rows($result);以便知道没有返回行进行查询。