Page 1 of 1

pulling sql data within a while loop

Posted: Tue Oct 12, 2010 10:46 am
by pinehead18
Goal here is to get the id's then pull the names of each of the id's from a different table. This works great until the result = db->query from within the while loop. When i apply that i only get 1 result, instead of 7 which is how many rows are really in the db.

Any input is appreciated.

Thanks,

Code: Select all

    $db = new MySQL(); 
    $result = $db->query("SELECT id FROM users WHERE user_id <> '$user_id'");
    
    while($row = $db->FetchArray($result)) {
     $id = $row['id'];

              
        $result =$db->query("SELECT * FROM chains WHERE id = '$id'");
          var_dump($chain_result);
        $row1 = $db->FetchArray($result);        
        $desc = $row1['description'];
        $name = $row1['name'];

        echo $id;
        echo "Name: " . $name . " <a href=\"#\" class=\"joinChain\">Join Chain</a>";
        echo "<form method=\"post\" action=\"\"><input type=\"hidden\" class=\"id\" value=\"". $id . "\">";
        echo "<br /> Description: " . $desc;
        echo "<br /><br />";
        
        } 

Re: pulling sql data within a while loop

Posted: Tue Oct 12, 2010 11:01 am
by AbraCadaver
How about just joining in the query and getting it all at once? Not tested but it's the general idea:

Code: Select all

$result = $db->query("SELECT * FROM users, chains WHERE users.id = chains.id AND users.user_id <> '$user_id'");

Re: pulling sql data within a while loop

Posted: Tue Oct 12, 2010 11:02 am
by Jonah Bron
The $result variable you're creating inside is interfering with the $result from outside. Change it to something else, like $result2.