user2331349
user2331349

Reputation: 25

PHP while returns same result repeatedly

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.

Upvotes: 1

Views: 471

Answers (4)

Rikesh
Rikesh

Reputation: 26421

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.

Upvotes: 1

Rudra
Rudra

Reputation: 364

<?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.

Upvotes: 1

bestprogrammerintheworld
bestprogrammerintheworld

Reputation: 5520

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.

Upvotes: 1

nvanesch
nvanesch

Reputation: 2600

<?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 />');
}
?>

Upvotes: 1

Related Questions