2

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,

  • Problem solved, culprit was an overloaded variable, note to others, check for overloaded variables :) –  Mar 29 '12 at 14:56

2 Answers2

0

Try using a while loop

E.g.

while ($row = mysql_fetch_assoc($building_types)) {
    echo $row['ColumnName'];
}
Bono
  • 4,757
  • 6
  • 48
  • 77
  • Hey, thanks, originally I used a while loop but when this didn't work I tried using a for loop to see if it would help me debug, I've fixed it now I think, culprit was an overloaded variable. Thanks anyway. –  Mar 29 '12 at 14:58
0

Bug was due to reuse of variable $building_types,