Jump to content

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


Go to solution Solved by requinix,

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:

  • Solution

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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.