简体   繁体   中英

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.

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

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 $data variable 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM