Recently in one of my project i wrote the following code for retrieving and print data from database.

$query = "select * from tblteachers limit 0, 4";
$result= mysql_query($query) or die(mysql_error());

if(mysql_num_rows($result)>0)
{   
    while($fetchRow=mysql_fetch_array($result, MYSQL_BOTH))
    {
      echo $fetchRow['id'];
    }
}

in the tblteachers table there are 14 row and the query get 4 row but the problem is it print 3 row by missing the first row. that is it print 2, 3, 4. How can i solve this?

Thanks in advanced.

link|flag

74% accept rate
@Arif - any chance first row id is null/empty ? – ajreal 2 days ago
id is auto increment in database. so it must exist. – Arif 2 days ago
1  
Can you paste in the output of the query when you run it from the mysql monitor? An auto_increment field does have to be non-null/primary key to work in MySQL, but if you applied the auto_increment later via alter table, after data had already been inserted, there could very well be a record with a null id. – Marc B 2 days ago
MYSQL_BOTH is useless, it is the second parameter's default value AND it clearly overheads the memory while it loads twice the same data. – Mathias E. 2 days ago
2  
How about modifying your inner loop to output more than just the row's id value? try something like echo 'howdy' . $fetchRow['id']. If you get four howdy's, you know the loop executed four times. As well, if you're viewing this in a browser, never forget to do a 'view source', as html/browsers will hide many things and make them appear invisible. – Marc B 2 days ago
show 2 more comments

Your Answer

 
or
never shown

Know someone who can answer? Share a link to this question via email, twitter, or facebook.

Browse other questions tagged or ask your own question.