Jump to content

Recommended Posts

KickStart;

 

I have just noticed that the mysql you provided me actually isn't working correctly :|.

 

it appears that it is just loading all friends of the users profile you are on, except the session user (which is how it should be), it doesn't only load friends that you both have in common. I thought it had been working perfectly before, but eh dunno what happened, I didn't change any of the code.

 

$mutual_friends = mysql_query("SELECT z.aFriend AS FriendId FROM (SELECT if(a.friend_1 = '$id',a.friend_2,a.friend_1) AS aFriend FROM friends a WHERE (a.friend_1 = '$id' AND a.friend_2 != '$session') OR (a.friend_2 = '$id' AND a.friend_1 != '$session')) z INNER JOIN (SELECT if(a.friend_1 = '$id',a.friend_2,a.friend_1) AS aFriend FROM friends a WHERE (a.friend_1 = '$id' AND a.friend_2 != '$session') OR (a.friend_2 = '$id' AND a.friend_1 != '$session')) x ON z.aFriend = x.aFriend ");

Hi

 

Not sure why I made that mistake.

 

Think (looking briefly) this should do it

 

$mutual_friends = mysql_query("
SELECT z.aFriend AS FriendId 
FROM (
SELECT if(a.friend_1 = '$id',a.friend_2,a.friend_1) AS aFriend 
FROM friends a 
WHERE (a.friend_1 = '$id' AND a.friend_2 != '$session') OR (a.friend_2 = '$id' AND a.friend_1 != '$session')) z 
INNER JOIN (
SELECT if(a.friend_1 = '$session',a.friend_2,a.friend_1) AS aFriend 
FROM friends a 
WHERE (a.friend_1 = '$session' AND a.friend_2 != '$id') OR (a.friend_2 = '$session' AND a.friend_1 != '$id')) x 
ON z.aFriend = x.aFriend ");

 

First subselect should get all the friends of $id who are not $session, while the 2nd is getting all friends of $session who are not $id. Then the JOIN brings that down to common friends.

 

This might be a bit cleaner (not tested though)

 

$mutual_friends = mysql_query("
SELECT z.aFriend AS FriendId 
FROM (
SELECT if(a.friend_1 = '$id',a.friend_2,a.friend_1) AS aFriend 
FROM friends a 
WHERE '$id' IN (a.friend_1,a.friend_2)) z 
INNER JOIN (
SELECT if(a.friend_1 = '$session',a.friend_2,a.friend_1) AS aFriend 
FROM friends a 
WHERE '$session' IN (a.friend_1,a.friend_2)) x 
ON z.aFriend = x.aFriend
WHERE z.aFriend NOT IN ('$id','$session')");

 

All the best

 

Keith

thank you, seems to work correctly now. Thanks a ton! for your help. I haven't tackled the level of mysql queries yet, which clearly I need to  :)

 

& ahh, I see the error now. Just switched session and id at the inner join. Glad I looked it over, I fixed the select posts from mutual friends that you made me earlier too. :D I'm a happy camper.

  • 1 month later...

Hi Guys

 

Sorry to jump in and restart this topic but it is very similar to a problem i am having and hopefully the answer lies within this one...

 

My query is very similar to the original posts (shortysbest) but he seems alot more confident with sql and Php than myself, and his 'friends' table is slightly different to mine as his uses a userid and mine is a user name.

 

Basically, my table looks like:

 

frienshipID-------friendname--------username

          1 -------------username1--------username2

 

etc

 

How can i dispay the friends of a logged in user??

 

 

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.