0

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.

8
  • @Arif - any chance first row id is null/empty ? Commented Dec 17, 2010 at 18:48
  • id is auto increment in database. so it must exist. Commented Dec 17, 2010 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. Commented Dec 17, 2010 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. Commented Dec 17, 2010 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. Commented Dec 17, 2010 at 19:17

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.