简体   繁体   中英

Mutual Friends PHP

So usually I wouldn't ask from help like this, so this is a first. But I've been racking my brains for two days now over a query that is most likely simple.

I'm wanting to get the count and the actual users printed out that our mutual to each other just like facebook.

My tables are as follows.

Friends id | user1_id | user2_id | status

User id | first | last

So I grasp the simple idea behind it. if I'm friends with someone, and they're also friends with that person that person becomes a mutual friend.

I've tried the below with no real results and tried so many other examples offline and to put it frank I don't understand inner-joins as I use them rarely so if someone could come up with something and possibly explain each part that may help me and others work out something for the future.

My session id is under $user1_id and user 2 is under $user2_id which are all sanitized against MYSQLI injection if this helps!

Thank you to anyone that can help me.

 $mutual_friends = mysqli_query($mysqli,"
SELECT user.id
FROM user
WHERE EXISTS(
    SELECT friends.user1_id, friends.user2_id, friends.status
    FROM friends 
    WHERE friends.user1_id = '$user1_id' AND friends.status = 2 
      AND friends.user2_id = user.id
  )
  AND EXISTS(
    SELECT friends.user1_id, friends.user2_id, friends.status
    FROM friends 
    WHERE friends.user1_id = '$user2_id' AND friends.status = 2 
      AND friends.user2_id = user.id
  )
");
$mutualfriend=mysqli_fetch_array($mutual_friends);
$mutualfriendcount=mysqli_num_rows($mutual_friends);
The mutual friend count by number
echo"".$mutualfriendcount.";

var_dump($mutual_friends );
while($mutualfriend=mysqli_fetch_array($mutual_friends)){

The list of users by name and id 

}
$mutual_friends = mysqli_query($mysqli,"
SELECT     user.user_id,
           user.first,
           user.last
FROM       user
INNER JOIN friends as friends1
       ON  friends1.user2_id = user.id
       AND friends1.user1_id = " . $user1_id . "
       AND friends1.status = 2
INNER JOIN friends as friends2
       ON  friends2.user2_id = user_id
       AND friends2.user1_id = " . $user2_id . "
       AND friends2.status = 2
");

Explanation:

First of all, your user_id columns are numerical, so you should not wrap their values in quotes.

SELECT ... FROM user has no secrets, it select all users.

Then the INNER JOIN with friends will find those records in friends that have the user1_id set to your connected user and express a friendship with the user record it is joined to. Now if there is no such record in friends , then the user record is filtered away from the result set. So in essence after the first INNER JOIN we are left with user records that correspond to the friends of the connected user.

Then the second INNER JOIN again tries to find friends, but with one difference. This time user1_id must match with the given $user2_id . So this is limited to friends of user #2. Now some of these friends were already filtered out from the result set after the first INNER JOIN was applied, so now we only reduce the result set further by taking out the users that are not friends of user #2.

And that gives the desired end result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM