mysql fetch array not iterating through the records

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BinaryBird
    New Member
    • Dec 2009
    • 1

    mysql fetch array not iterating through the records

    Hi, i have a small script to display records from two tables. Table 1 keeps track of teams and table 2 keeps track of the members.
    Code:
    <?php
    $dl = mysql_connect ("localhost","nitin_nick","test1234");
    $db = mysql_select_db("nitin_registration",$dl);
    
    echo "<table border='0' width='986' align='center' cellspacing='1' cellpadding='5'>";
    echo "<tr>";
    
    echo "<th>Team Name</th>";
    echo "<th>Team Contact</th>";
    echo "<th>Member Name</th>";
    echo "<th>Member Email</th>";
    echo "<th>Mobile Phone</th>";
    echo "<th>College</th>";
    echo "</tr>";
    $result1="";
    $result2="";
    $query1 = "SELECT * FROM team ";
    $q1_set = mysql_query($query1,$dl);
    
    while ($result1 = mysql_fetch_array($q1_set)) {
       $team_id = $result1['id'];
       echo "<tr>";
       echo "<td>".$result1['name']."</td>";
       echo "<td>".$result1['contact']."</td>";
       echo "<td>".""."</td>";
       echo "<td>".""."</td>";
       echo "<td>".""."</td>";
       echo "<td>".""."</td>";
       echo "</tr>";
       $query2 = "SELECT * FROM member WHERE team_id={$team_id} LIMIT 3";
    
       $q2_set = mysql_query($query2,$dl) ;
      
       $result2 = mysql_fetch_array($q2_set);
         $count=0;
     
       while ($result2 && $count<3)  {   //The index of $result2 is not incrementing
            echo "<tr>";
            echo "<td>".""."</td>";
            echo "<td>".""."</td>";
            echo "<td>".$result2['name']."</td>";
            echo "<td>".$result2['email']."</td>";
            echo "<td>".$result2['phone']."</td>";
            echo "<td>".$result2['college']."</td>";
            echo "</tr>";
            $count++ ;
    
       } 
    }
    
    
    echo "</table>";
    
    ?>
    The second query where i am trying to pull members with the team id, the result of mysql_fetch_arr ay() is not incrementing the index of the array. When i run the script its goes into a infinite loop,( the second while loop) It just has the first record . Have i done something wrong here? Kindly help me out. Thanks.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    yepp, you do your second loop inside the first loop, thus the variable of the first loop is not incremented, as long as the second loop didn’t finish.

    Comment

    Working...