first row not printing with mysql_fetch_array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zachrose
Forum Newbie
Posts: 5
Joined: Mon Jun 16, 2008 12:34 pm

first row not printing with mysql_fetch_array

Post by zachrose »

Hi all,

I seem to be having some trouble understanding mysql_fetch_array(). I've got a script and for some reason it seems to be skipping the first mysql result row but doing fine with every one after that.

Here's the code:

Code: Select all

 
$list_query = "SELECT first_name, last_name, students.student_id
 
FROM enrollment, students
 
WHERE enrollment.student_id = students.student_id 
AND date >= '$date_start'
AND date < '$date_end'
ORDER BY date";
 
if (!($list_result = @ mysql_query($list_query, $connection)))
    die (showerror());
$list_info = mysql_fetch_array($list_result);
if (empty($list_info))
    { echo '<tr><td class=body colspan=7 align=center>There were no registrations for this date.</td></tr>'; }
else 
    {
    echo $list_info[0];
    while ($list_info = mysql_fetch_array($list_result))  //why doesn't this return the first row?
        {
            $first_name = $list_info['first_name'];
            ...and so on assigning array elements to variables and then echoing them with formating.
 
If I sort the MySQL query by date, the earliest row doesn't print. If I sort it by first_name, Beth and Carol will print but not Albert.

I think this is a problem with mysql_fetch_array(), but I have no idea why.

Any thoughts?
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: first row not printing with mysql_fetch_array

Post by jaoudestudios »

You are fetching the array twice, you only need to do it once.

Line 13 is your problem!
Post Reply