First record in mysql_fetch_array is skipped?!
Posted: Sun Mar 11, 2007 11:02 am
I'm populating a list box with the results of mysql_query. (In the code below, I've commented out that section in favor of simply echoing the records to the screen, line by line.)
mysql_numrows returns the correct number of rows, and the query has been verified in phpmyadmin, but when the results are viewed, the first record is not echoed. If 2 records are returned, only the 2nd is echoed. If 3 records are returned, only the 2nd and 3rd are echoed. Etc.
The basic code for this is used on several pages of the site, and it works fine everywhere else. I'm really stymied as to what could be causing this. I could probably implement a workaround using mysql_data_seek, but I'd really like to know what the heck is going on here! Note in the code below that the sections for "if $num==0" and "else" ($num = 1) both work fine - it's just the elseif section (where $num (number of records returned) is > 1).
Thanks for taking the time to look at this!
mysql_numrows returns the correct number of rows, and the query has been verified in phpmyadmin, but when the results are viewed, the first record is not echoed. If 2 records are returned, only the 2nd is echoed. If 3 records are returned, only the 2nd and 3rd are echoed. Etc.
The basic code for this is used on several pages of the site, and it works fine everywhere else. I'm really stymied as to what could be causing this. I could probably implement a workaround using mysql_data_seek, but I'd really like to know what the heck is going on here! Note in the code below that the sections for "if $num==0" and "else" ($num = 1) both work fine - it's just the elseif section (where $num (number of records returned) is > 1).
Thanks for taking the time to look at this!
Code: Select all
$lastname = $_POST['lastname'];
$query = "SELECT *
FROM alumni
WHERE Last='$lastname'";
$result = mysql_query($query,$dblink) or die(mysql_error());
$num = mysql_numrows($result);
$row = mysql_fetch_array($result);
if ($num == 0) { // ALUMNUS NOT IN DATABASE
echo "<h1><br /></h1><h3>DHS57.com Administration - Classmate Lookup</h3>";
echo "<h4>Sorry, no classmates found with the last name '$lastname'.</h4>";
include 'return.inc';
echo "<input type='hidden' name='auth' value='$auth'></form>";
}
elseif ($num > 1){ // MULTIPLE RECORDS MATCHED - CHOOSE ONE
/* echo "<h3>Select Classmate</h3>
<form action='admin.php' method='post'>
<table border='0' align='center'>
<tr valign='middle'><td>";
<select name='alum_select' size='$num'>";
while($row = mysql_fetch_array($result)) {
$userID = $row['ID'];
$firstname = $row['First'];
$lastname = $row['Last'];
echo "<option value='".$userID."'>".$firstname." ".$lastname."</option>";
echo "$userID - $firstname $lastname \n";
}
echo "</select><br /><br /><input class='inlinebutton' type='submit' name='submit' value='lookup'>";
</td></tr></table>
<input type='hidden' name='proc' value='sel_alum'>
<input type='hidden' name='auth' value='$auth'></form>";
*/
while($row = mysql_fetch_array($result)) {
$userID = $row['ID'];
$firstname = $row['First'];
$lastname = $row['Last'];
echo "$userID $firstname $lastname <br />";
}
}
else { // EDIT ALUMNUS RECORD
$cm_id = $row['ID'];
// echo "edit record for $first $last";
include 'modrec.php';
echo "<input type='hidden' name='auth' value='$auth'></form>";
}
break;