简体   繁体   中英

How to select mutual friends

please i want to select mutual friends of a user and the $my_id is the $_SESSION['user']; while the $user_id is the selected user. here is the code:

    <?php

include 'connect.php';

include 'functions.php';

$my_frnds = mysqli_query{$con, "SELECT * FROM friends WHERE one='$my_id' OR two='$my_id'"};
while($my_friends = mysqli_fetch_array($my_frnds)){
    $one = $my_friends['one'];
    $two = $my_friends['two'];

    if($one == $my_id){
        $user == $two;
    } else
        $user == $one;
}

$user_frnds = mysqli_query{$con, "SELECT * FROM friends WHERE one='$user_id' OR two='$user_id'"};
while($user_friends = mysqli_fetch_array($user_frnds)){
    $user_one = $user_friends['one'];
    $user_two = $user_friends['two'];

    if($user_one == $my_id){
        $users == $user_two;
    } else
        $users == $user_one;
}

?>

lets assume that my friends are (1, 2, 3, 4, 5, 6, 7) and the user friends are (2, 4, 6, 7, 8, 9, 10) so the mutual friends are (2, 4, 6, 7) my question is how do i get the mutual friend out and echo thier ids?. Sorry i made a mistake, here is the original coding

in case one is your id and two is friend id:

select * from friends 
inner join (select * from friends where one = $user_id) as temp on temp.two = friends.two
where friends.one = $my_id 
    SELECT * FROM friends AS f1
    INNER JOIN 
    (SELECT * FROM friends WHERE one = $user_id OR two = $user_id) AS f2 
    ON (f1.one = f2.one OR f1.one = f2.two) 
    WHERE f1.one = $my_id OR f1.two = $my_id

hope this helps.

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