php while loop

Associate
Joined
18 Sep 2003
Posts
2,368
I'm running a while loop to spit some info out from my database. The problem is the code isn't displaying the very first item in the array - does anyone know why?

<?
// GETS bookies from DB
$query = ("SELECT * FROM bookmakers WHERE status='active' AND type='Sportsbook' ORDER BY id_name");
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$row = mysql_fetch_array($result);

while ($row = mysql_fetch_array($result)) {
?>


<tr valign="middle">
<td class="left-link"><a href="sports/<? echo $row["id_name"];?>.php" title="<? echo $row["bookmaker"];?>"><? echo $row["bookmaker"];}?></a></td>
</tr>

Thanks :)
 
Last edited:
You need to remove $row = mysql_fetch_array($result); from before the loop. Remember, the condition for the while loop is evaluated before every iteration, including the first, so if you've already got the row it will be discarded.
 
Out of interest (I'm learning), would foreach work like this?
Code:
<?
// GETS bookies from DB
$query = ("SELECT * FROM bookmakers WHERE status='active' AND type='Sportsbook' ORDER BY id_name");
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

foreach($result as $row){
?>


<tr valign="middle">
<td class="left-link"><a href="sports/<? echo $row["id_name"];?>.php" title="<? echo $row["bookmaker"];?>"><? echo $row["bookmaker"];}?></a></td>
</tr>
Ta
 
Your way will probably work. I tend to use the method below. Not so sure why I use it apart from that it was the way used in the book(s) I learnt PHP from.

Code:
function view_all_staff()
{
	$staff = db_get_staff_all();
	$staff_numrows = querynumrows($staff);
	print '<h1>All Employees</h1>';
	print '<div id="chwlist"><table align="center" border="0">';
	print '<thead><tr><th scope="col">First Name</th><th scope="col">Last Name</th><th scope="col">Department</th><th scope="col">Tel Extension No.</th><th scope="col">Desk / Location</th></tr></thead>';
	print '<tfoot><tr><th scope="row">Total</th><td colspan="4">'.$staff_numrows.' Employees</td></tr></tfoot>';
	for ($i=0; $i <$staff_numrows; $i++)
	{
		$staff_asoc = query_asoc($staff);
		print '<tr><td align="center">'.$staff_asoc['FirstName'].'</td>';
		print '<td align="center">'.$staff_asoc['LastName'].'</td>';
		print '<td align="center">'.$staff_asoc['Dept_Name'].'</td>';
		print '<td align="center">'.$staff_asoc['ExtNumber'].'</td>';
		print '<td align="center">'.$staff_asoc['OfficeArea'].', '.$staff_asoc['OfficeLocation'].'</td>';
		print '</tr>';
	}		
	print '</table></div>';
	print '<br><br>';
}
 
Last edited:
In code please, not php. Impossible to read like that :)

Edit: yes a for loop is another way. I seem to remember that foreach is the quickest, then while, then for. Not sure about that, but that's why I suggest a foreach.
 
Inquisitor said:
You need to remove $row = mysql_fetch_array($result); from before the loop. Remember, the condition for the while loop is evaluated before every iteration, including the first, so if you've already got the row it will be discarded.
Thank you, a simple resolution indeed - I can see what I was doing wrong.
 
joeyjojo said:
In code please, not php. Impossible to read like that :)

Edit: yes a for loop is another way. I seem to remember that foreach is the quickest, then while, then for. Not sure about that, but that's why I suggest a foreach.


Another way to do it but not used very often is:

Code:
while(list($k,$v) = each($array))
{
   //loop through the contents here
}

The benefit of this over the foreach is that foreach creates a copy of the original array so if you've got a very large results array it could cause performance problems. But generally I use either while when looping through results sets or foreach when using arrays.
 
Back
Top Bottom