当mysql_num_rows说有数据时,mysql_fetch_array返回false,原因是什么


mysql_fetch_array returning false when mysql_num_rows says that there is data, why?

我的表有10条记录,mysql_num_rows说mysql资源中有10行,在phpMyAdmin中我可以看到10行,但是当调用mysql_fetch_array时,前两次它正常工作,最后8次它返回FALSE。

为什么?

$query = "SELECT * FROM building_types";
$building_types = mysql_query($query) or die( mysql_error() );// works
echo mysql_num_rows($building_types); // prints 10
$num_rows = mysql_num_rows($building_types); 
for ( $i = 0 ; $i < $num_rows ;$i++ )
{
  echo"hi1"; // this is printed 10 times
  $building_type = mysql_fetch_array($building_types);
  echo $building_type; // prints Array 2 times not 10 times ...
  if ( $building_type === FALSE ) echo"hi2"; //this is printed the last 8 times ...

谢谢,

尝试使用while循环

例如

while ($row = mysql_fetch_assoc($building_types)) {
    echo $row['ColumnName'];
}

错误是由于重复使用变量$building_types,