Jump to content

mysql_fetch_assoc() Only returning 1 value instead of all of them.


MstrGmrDLP

Recommended Posts

Requesting help from anyone who is a geek. :geek:

My code is checking all of the friends where the logged in user is user_id. It gets all of the values but only displays 1. What am I doing wrong?


Code

 

$friend = mysql_query("SELECT * FROM friends WHERE user_id={$_SESSION['user']['user_id']}");
$row = mysql_fetch_assoc($friend);
$friends = mysql_query("SELECT user_name, user_pic, user_id FROM users WHERE user_id={$row['friend_user_id']}");

 

 

 

 Then later on I have a while loop getting this information. But it only displays 1 users information. Why won't it show them all?

If you could help I would be really happy :happy-04:

Because you only got one row. If you want all of them then you need to put the mysql_fetch_assoc() stuff into its own while loop.

 

But that's not very efficient. You could just execute one query to get all the user information for all the friends.

SELECT users.user_name, users.user_pic, users.user_id
FROM users
JOIN friends ON users.user_id = friends.friend_user_id
WHERE friends.user_id = that id you have in the session
One while loop.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.