3

everything works fine, but the output is being listed twice.

so it echos out: 'output a' 'output a' 'output b' 'output b' 'output c' 'output c' and so on.

when i do a mysql query in phpmyadmin, everything is only listed once. any ideas?

what my code is doing is going and getting a many-to-many field

<? 
    $something = mysql_query('select UserID from users where Username ="'. $username .'"');
    while ($row = mysql_fetch_array($something)) {
        $barf = $row['UserID'];
    }
    $result = mysql_query('SELECT name FROM items p LEFT JOIN list up ON p.item_id = up.item_id WHERE up.UserID =  "' . $barf . '"');
    while ($r = mysql_fetch_array($result)) {
        foreach($r as $uue) {
            echo $uue . '<br>';}
        }
    }
?>
Matej
  • 9,548
  • 8
  • 49
  • 66
Ghost Echo
  • 1,997
  • 4
  • 31
  • 46
  • what does `var_dump($r);` (put into the while loop) print? – Matej May 09 '13 at 20:39
  • 1
    you don't need 2 queries, it could be done in one –  May 09 '13 at 20:42
  • 2
    You need to read up on [proper SQL escaping](http://bobby-tables.com/) because you cannot write code like this and expect it to be safe. – tadman May 09 '13 at 20:43

3 Answers3

7

Try this code:

while ($r = mysql_fetch_assoc($result)){
  foreach($r as $uue) {
    echo $uue . '<br>';
  }
}

You get duplicate results because by default (MYSQL_BOTH flag) mysql_fetch_array returns 2x sized array : number-indexed + string-indexed.

So, for example $r[0] and $r['name'] will be different elements in the array that contain the same values.

Also, you should stop using mysql_* as it is deprecated. Read on mysqli_* functions.

sybear
  • 7,837
  • 1
  • 22
  • 38
2

Here is an example using a single query with a sub query. No need to do 2 loops when mysql can give you the data you need on the first run. Also the extra foreach loop in your while isn't needed since your data is being returned as an array of columns. You can just echo what you want by index. Since you are only pulling one column you can echo $r['name'] or if you do MYSQL_NUM you can do $r[0].

<?
$escaped_username = mysql_real_escape_string($username);
$query = <<<SQL
SELECT
    name
FROM
    items p
    LEFT JOIN list up ON p.item_id = up.item_id
WHERE
    up.UserID = (SELECT UserID FROM users WHERE Username = '{$escaped_username}')
SQL;

$result = mysql_query($query);
while ($r=mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo $r['name'] . '<br />';
}
?>

Also using mysqli would probably be a better route to take as others have said. You can go the procedural route which would be pretty much replacing mysql_ with mysqli_ or you can go the object oriented route.

<?    
$mysqli = new mysqli('host', 'user', 'pass', 'database');
$result = $mysqli->query($query);

while ($r = $result->fetch_object()) {
    echo $r->name . '<br />';
}
?>
DSlagle
  • 1,563
  • 12
  • 19
0

regardless of your true answer which is for @jari i think, Why you don't use just a simply echo $r[0]. '<br>'; or echo $r['name']. '<br>'; instead of that extra foreach

Amir
  • 4,089
  • 4
  • 16
  • 28
  • I think i need the `foreach` because $r['name'] won't always have the same number of variables to output for each user. Sometimes it could be 0, sometimes 1,2,3, etc – Ghost Echo May 10 '13 at 12:10
  • each iterate of while loop, $r will be one element array like this `(0=>'name')` and if you need to echo name , just `echo $r['name'];` – Amir May 10 '13 at 14:38