Mysql_fetch_array skips the first row

EDIT: Thanks for the quick answers guys - I switched to mysql_fetch_assoc () and used do ... while, and I'm good to go.

I use the same process to create and populate tables. Everything works fine, except that the first row is skipped in each table . May use a different pair of eyes, as I cannot pinpoint the problem. Thanks in advance.

$query = "SELECT * FROM members"; $results = mysql_query($query); $row = mysql_fetch_array($results); //echo my <table> start and headings; while ($row = mysql_fetch_array($results)) { echo "<tr><td>".$row['Last Name']."</td>"; echo "<td>".$row['First Name']."</td>"; echo "<td>".$row['Middle Name']."</td>"; echo "<td>".$row['Sfx']."</td>"; echo "<td>".$row['Prf']."</td>"; echo "<td>".$row['Spouse/SO']."</td>"; echo "<td>".$row['Ancestor']."</td>"; echo "<td>".$row['Status']."</td>"; echo "<td>".$row['Address 1']."</td>"; echo "<td>".$row['Address 2']."</td>"; echo "<td>".$row['City']."</td>"; echo "<td>".$row['ST']."</td>"; echo "<td>".$row['Zip 5']."</td>"; echo "<td>".$row['Zip 4']."</td>"; echo "<td>".$row['Home Phone']."</td>"; echo '<td><a href="mywebsite/mypage.php?id=' . $row['id'] . '">Bio</a></td></tr>'; } echo "</table><hr>"; 
+4
source share
5 answers

You call mysql_fetch_array twice ... once before the loop, then once during the loop.

If you need a seed string to use when building a title bar, you might be better served with do .. while loop here.

 $query = "SELECT * FROM members"; $results = mysql_query($query); $row = mysql_fetch_assoc($results); //echo my <table> start and headings; do { echo "<tr><td>".$row['Last Name']."</td>"; echo "<td>".$row['First Name']."</td>"; echo "<td>".$row['Middle Name']."</td>"; echo "<td>".$row['Sfx']."</td>"; echo "<td>".$row['Prf']."</td>"; echo "<td>".$row['Spouse/SO']."</td>"; echo "<td>".$row['Ancestor']."</td>"; echo "<td>".$row['Status']."</td>"; echo "<td>".$row['Address 1']."</td>"; echo "<td>".$row['Address 2']."</td>"; echo "<td>".$row['City']."</td>"; echo "<td>".$row['ST']."</td>"; echo "<td>".$row['Zip 5']."</td>"; echo "<td>".$row['Zip 4']."</td>"; echo "<td>".$row['Home Phone']."</td>"; echo '<td><a href="mywebsite/mypage.php?id=' . $row['id'] . '">Bio</a></td></tr>'; } while ($row = mysql_fetch_assoc($results)); echo "</table><hr>"; 
+10
source
 $query = "SELECT * FROM members"; $results = mysql_query($query); $row = mysql_fetch_array($results); <-- There your first row. Remove this. 
+5
source

There is additional mysql_fetch_array in the code. In addition, there is no open table tag ( <table> ).

Corrected Code:

 $query = "SELECT * FROM members"; $results = mysql_query($query); echo "<table>"; while ($row = mysql_fetch_array($results)) { echo "<tr><td>".$row['Last Name']."</td>"; echo "<td>".$row['First Name']."</td>"; echo "<td>".$row['Middle Name']."</td>"; echo "<td>".$row['Sfx']."</td>"; echo "<td>".$row['Prf']."</td>"; echo "<td>".$row['Spouse/SO']."</td>"; echo "<td>".$row['Ancestor']."</td>"; echo "<td>".$row['Status']."</td>"; echo "<td>".$row['Address 1']."</td>"; echo "<td>".$row['Address 2']."</td>"; echo "<td>".$row['City']."</td>"; echo "<td>".$row['ST']."</td>"; echo "<td>".$row['Zip 5']."</td>"; echo "<td>".$row['Zip 4']."</td>"; echo "<td>".$row['Home Phone']."</td>"; echo '<td><a href="mywebsite/mypage.php?id=' . $row['id'] . '">Bio</a></td></tr>'; } echo "</table><hr>"; 
+1
source

You call mysql_fetch_array when initializing $ row, which reads the first row. Then you call it a second time when you start the while loop, which moves it to the second line before any of your loops are started. Therefore, he never does anything with the first line.

0
source
 while ($row) { echo "<tr><td>".$row['Last Name']."</td>"; echo "<td>".$row['First Name']."</td>"; echo "<td>".$row['Middle Name']."</td>"; echo "<td>".$row['Sfx']."</td>"; echo "<td>".$row['Prf']."</td>"; echo "<td>".$row['Spouse/SO']."</td>"; echo "<td>".$row['Ancestor']."</td>"; echo "<td>".$row['Status']."</td>"; echo "<td>".$row['Address 1']."</td>"; echo "<td>".$row['Address 2']."</td>"; echo "<td>".$row['City']."</td>"; echo "<td>".$row['ST']."</td>"; echo "<td>".$row['Zip 5']."</td>"; echo "<td>".$row['Zip 4']."</td>"; echo "<td>".$row['Home Phone']."</td>"; echo '<td><a href="mywebsite/mypage.php?id=' . $row['id'] . '">Bio</a></td></tr>'; $row = mysql_fetch_array($results); } echo "</table><hr>"; 
0
source

Source: https://habr.com/ru/post/1497297/


All Articles