当mysql_num_rows表示有数据时,mysql_fetch_array返回false,为什么?

时间:2012-03-29 13:27:43

标签: php mysql phpmyadmin mysql-num-rows

我的表有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 ...

谢谢,

2 个答案:

答案 0 :(得分:0)

尝试使用while循环

E.g。

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

答案 1 :(得分:0)

Bug是由于重用了$ building_types,