0

I am trying to use this query to return every instance where the variable $d['userID'] is equal to the User ID in a separate table, and then echo the username tied to that user ID.

Here's what I have so far:

$uid = $d['userID'];

$result = mysql_query("SELECT u.username
FROM users u
LEFT JOIN comments c
ON c.userID = u.id
WHERE u.id = $uid;")$row = mysql_fetch_assoc($result);
echo $row['username'];
user547794
  • 14,263
  • 36
  • 103
  • 152

3 Answers3

2

This should work

$uid = mysql_real_escape_string($d['userID']);

$result = mysql_query("SELECT u.username
FROM users u
LEFT JOIN
 comments c
ON c.userID = u.id
WHERE u.id = '$uid'");


    while($row = mysql_fetch_assoc($result))
{
   //PRINTS ALL INSTANCES OF THE ROW
    echo $row['username'];
}

what is printed from the echo above.

Johan
  • 74,508
  • 24
  • 191
  • 319
tkt986
  • 1,081
  • 3
  • 17
  • 25
0

Try this:

$uid = $d['userID'];

$result = mysql_query("SELECT u.username
FROM users u
LEFT JOIN comments c
ON c.userID = u.id
WHERE u.id = $uid");
while($row = mysql_fetch_assoc($result))
{
  echo $row['username'];
}

*EDIT:*Removed semicolon.

Chandu
  • 81,493
  • 19
  • 133
  • 134
0

if you're looking to get all the results. wrap your mysql_fetch_assoc in a while loop:

$result = mysql_query("SELECT u.username
FROM users u
LEFT JOIN comments c
ON c.userID = u.id
WHERE u.id = $uid;");
while($row = mysql_fetch_assoc($result)){
    echo $row['username'];

}
seanh
  • 214
  • 1
  • 7