While loop not echoing first row of data

Discussion in 'PHP' started by gtrufitt, Mar 16, 2008.

  1. #1
    Hi, I have a while loop that runs through the array but it doesnt echo the first row of data, it starts from the second. Any ideas why?
    My code is

    
    <?php
    $getfriends = "SELECT friendid FROM friends WHERE id = '$user_id'";
    $gf = mysql_query($getfriends) or die();
    $friendrow = mysql_fetch_array($gf);
    
    ?>
    Code (markup):
    <?php
      
      if (mysql_num_rows($gf) == 0) {
    	echo $f_name . ' has no friends!' ;
    } 
    else 
    { 
    while ($rowgf = mysql_fetch_array($gf, MYSQL_ASSOC)) {
    $getfriendid = $rowgf['friendid'];
    $getfname = "SELECT f_name, l_name FROM user WHERE id = '$getfriendid'";
    $gfn = mysql_query($getfname) or die();
     $rowf = mysql_fetch_array($gfn, MYSQL_ASSOC);
      {
    		
    echo ($rowf["f_name"]);
    echo ($rowf["l_name"]);
    
    }
    }
    }
    ?>		
    Code (markup):
    thanks,

    Gareth
     
    gtrufitt, Mar 16, 2008 IP
  2. gtrufitt

    gtrufitt Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Nope that didnt work, any other ideas?
     
    gtrufitt, Mar 16, 2008 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    Remove this line in the first block of code:
    
    $friendrow = mysql_fetch_array($gf);
    
    PHP:
    This fetches the first row, and the loop continues from the second. If you need this line there, then use a do-while loop.

    www.php.net/control-structures.do.while
     
    nico_swd, Mar 16, 2008 IP
  4. gtrufitt

    gtrufitt Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ace, that fixed it, cheers.
     
    gtrufitt, Mar 16, 2008 IP