如何获取相互朋友并在循环中显示结果

时间:2012-02-25 16:47:55

标签: php mysql sql

我遇到很多问题,在用户之间找到共同的朋友,并在while循环中显示这些共同朋友的图片,用户ID和名称。我已经尝试过几乎关于共同朋友的所有答案,但是当我混合while循环所需的所有列时,一切都会崩溃。任何帮助将不胜感激。

朋友表:friend_id, friend_one, friend_two, date

用户表:uid, name_f, name_l, profile_pic

当用户互相添加为朋友时,我会记录以下内容。我们假设用户1和用户2现在是朋友:

friend_one: 1, friend_two: 2, Date

friend_one: 2, friend_two: 1, Date

我需要类似下面的内容,sql查询只返回我的共同朋友的数量但不帮助我选择用户的uid,个人资料图片和名称:

$friends_sql=mysql_query("SELECT b.friend_two FROM (SELECT friend_two FROM friends WHERE friend_one = $myvar1) a INNER JOIN (SELECT friend_two FROM friends WHERE friend_one = $myvar2) b ON a.friend_two = b.friend_two");
                while($friends=mysql_fetch_array($friends_sql))
                {
                something
                ?>
                <li>


 <img of user><p>name of user</p><a hre>link to user's uid</a>
</li>

                <?php
                }
                ?>

1 个答案:

答案 0 :(得分:2)

下一个代码是否适合您?

$friends_sql=mysql_query("SELECT b.friend_two FROM (SELECT friend_two FROM friends WHERE friend_one = $myvar1) a INNER JOIN (SELECT friend_two FROM friends WHERE friend_one = $myvar2) b ON a.friend_two = b.friend_two");
                while($friends=mysql_fetch_array($friends_sql))
                {
                     $getMutualFriendInfo = mysql_query("SELECT * FROM users WHERE uid='{$friends['friend_two']}'");
                      $MutualFriendInfo = mysql_fetch_array($getMutualFriendInfo);
                ?>
                <li>


 <img src="<?=MutualFriendInfo['profile_pic']?>"><p><?=MutualFriendInfo['name_f'] ?> <?=MutualFriendInfo['name_l'] ?></p><a href="/user.php?uid=<?=MutualFriendInfo['uid'] ?>"></a>
</li>

                <?php
                }
                ?>