1

When running a while script on my local xampp it just returns the same result over and over again, here's the PHP I'm using:

<?php
$id = $_GET['id'];
$getmembers = mysql_query("SELECT * FROM group_memberships WHERE groupid = $id LIMIT 10");
$getids = mysql_fetch_assoc($getmembers);
    while($id = $getids['userid']){
        $getusers = mysql_fetch_assoc(mysql_query("SELECT look, username FROM users WHERE id = '$id'"));
        echo ($getusers['username']);
        echo('<br />');
    }
?>

All that I get when I execute that is: http://prntscr.com/12nh6g My database contains 4 different users registered to that group. I'm not sure about joining tables in MySQL as I have no idea how to do it.

If I add a break; then it only returns one result, instead of timing out.

Any help is much appreciated!

Also I'm sorry about how messy the PHP is, I am new to this.

4 Answers4

1
while($id = $getids['userid']){ #This condition will always be true.

Use your mysql_fetch_assoc statement in a loop like below.

while ($getids = mysql_fetch_assoc($getmembers)) {
    $id = $getids['userid']
    $getusers = mysql_fetch_assoc(mysql_query("SELECT look, username FROM users WHERE id = '$id'"));
    echo $getusers['username'];
    echo '<br />';
}

For sub query you can try,

SELECT look, username FROM users
WHERE id IN 
       (SELECT userId FROM group_memberships
        WHERE groupid = $id
        LIMIT 10)

Using JOIN,

SELECT u.look,u.username
FROM users as u
LEFT JOIN group_memberships as gm
     on gm.userId = u.id 
     WHERE gm.groupid = $id
     LIMIT 10 

Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rikesh
  • 26,156
  • 14
  • 79
  • 87
  • Glad to help you. Please don't forget to accept my answer by clicking the checkmark to the left of it. – Rikesh Apr 29 '13 at 09:16
1
<?php

$id = (int) $_GET['id']; // very important part here, I cast the id to int, else you have a SQL injection leak
$getmembers = mysql_query("SELECT * FROM group_memberships WHERE groupid = $id LIMIT 10");
while ($membership = mysql_fetch_assoc($getmembers)) // on each pass we take a new result off first query
{
    $user = mysql_fetch_assoc(mysql_query("SELECT look, username FROM users WHERE id = '".$membership['user_id']."'"));
    echo ($user['username']);
    echo('<br />');
}
?>
nvanesch
  • 2,540
  • 14
  • 25
1

In your code:

<?php
$id = $_GET['id'];
$getmembers = mysql_query("SELECT * FROM group_memberships WHERE groupid = $id LIMIT 10");
$getids = mysql_fetch_assoc($getmembers);
    while($id = $getids['userid']){
        $getusers = mysql_fetch_assoc(mysql_query("SELECT look, username FROM users WHERE id = '$id'"));
        echo ($getusers['username']);
        echo('<br />');
    }
?>

tells PHP that "Do the loop" while $id is ASSIGNED to the value of $getids['userid']

Therefore you you will have a infinite loop because $id is assigned all the time (and always with the same value) $getids are never changed inside of the while-loop.

But even if $getids was about to change it would still be in an infinite loop because the loop isn't checking if $id IS value of $getids['userid'], it's just assigning the value.

bestprogrammerintheworld
  • 5,417
  • 7
  • 43
  • 72
1
<?php
$id = $_GET['id'];
$getmembers = mysql_query("SELECT * FROM group_memberships WHERE groupid = $id LIMIT 10");

    while($getids = mysql_fetch_assoc($getmembers)){
    $getusers = mysql_fetch_assoc(mysql_query("SELECT look, username FROM users WHERE id = '$id'"));
    echo ($getusers['username']);
    echo('<br />');
    }
?>

you had the while loop wrong , you need to do the feching inside while loop , so it goes till the fetching stop.

Rudra
  • 354
  • 1
  • 2
  • 10