phpmysqlphpmyadminmysql-num-rows

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


my table has 10 records, and mysql_num_rows says that there are 10 rows in the mysql resource, in phpMyAdmin I can see 10 rows, but when mysql_fetch_array is called, the first two times this works correctly, then the last 8 times it returns FALSE.

Why?

$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 ...

Thanks,


Solution

  • Bug was due to reuse of variable $building_types,