Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
 
@Arif - any chance first row id is null/empty ? –  ajreal Dec 17 '10 at 18:48
 
id is auto increment in database. so it must exist. –  Arif Dec 17 '10 at 18:52
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 Dec 17 '10 at 19:07
 
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. Dec 17 '10 at 19:09
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 Dec 17 '10 at 19:17
show 2 more comments

1 Answer

you might have used something that moved the pointer. try adding mysql_data_seek($result,0); before the while statement

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.