user2527750 user2527750 - 4 months ago 7
MySQL Question

Select * from table always misses first row result

I expect this is a simple coding error.
When trying to get all results from database table, the first result is always missed off. Even when adding and removing information to the database it will always miss off the result with the lowest ID.

$data = mysql_query("SELECT * FROM tbl_messages")
or die(mysql_error());

$info = mysql_fetch_array( $data );

while($info = mysql_fetch_array( $data ))
{
Print "<a href=\"../message/index.php?user=\">" .$info['subject']. "</a>";
echo "<hr>";
}

Answer

Remove the first mysql_fetch_array statement.

something like

$data = mysql_query("SELECT * FROM tbl_messages") 
 or die(mysql_error()); 

 while($info = mysql_fetch_array( $data )) 
 { 
 Print "<a href=\"../message/index.php?user=\">" .$info['subject']. "</a>";
  echo "<hr>";
 } 

You are skipping the first entry by using the code as above.

From mysql_fetch_array

Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

Which means that when you get to the while, you have already read the first row, but not used it yet.