Nandu
Nandu

Reputation: 231

1st row is not being from database in php

For some reason I'm having a problem retrieving data from my database. It leaves off the first item being listed.

$sql=mysql_query("SELECT * FROM students WHERE (year = '" . mysql_real_escape_string($_SESSION['year']) . "') and ( branch= '" . mysql_real_escape_string(($_SESSION['branch'])). "') ");


$data=mysql_fetch_array( $sql );

print "<table>"
while($data = mysql_fetch_array( $sql )) 
 { 

 Print "<tr><td>".$data['idno']." </td><td>".$data['name'] . " </td></tr>";
 } 
print "</table>"

Please help me with this. Thank you.

Upvotes: 1

Views: 59

Answers (2)

mAu
mAu

Reputation: 2018

Remove the following line:

$data=mysql_fetch_array( $sql ); 

The call to mysql_fetch_array moves the internal pointer to the next row, thus you are getting all rows except the first in your while loop.

You could also reset the internal pointer with mysql_data_seek.

mysql_data_seek ($sql, 0); // 0 for first row

Upvotes: 3

artragis
artragis

Reputation: 3713

It's because you use mysql_fetch_array one time before your while. The first call will get the first result, and then you will enter into the while loop. the $datavariable will erase the first result to be assigned by the second result and then the third, the fourth and so on. Because of this call before the while loop, you will always avoid the first result

Upvotes: 0

Related Questions