我在 php.ini 中创建了一个“你可能认识的人”脚本。这个脚本向我展示了我朋友的朋友,在一个 php 朋友系统中。我有一个名为users (user_id, name, surname, email, profile)
thats 的表,其中包含有关用户的信息。另一个名为friends(friend_id, user_one, user_two)
保存朋友用户 ID 的表。我的代码如下:
<?php
// ------ gives me my friends
$friend_query = mysql_query(" SELECT `user_one`, `user_two` FROM `friends` WHERE `user_one`='$session_user_id' OR `user_two`='$session_user_id' ");
while($run_friend = mysql_fetch_array($friend_query)){
$user_one = $run_friend['user_one'];
$user_two = $run_friend['user_two'];
if($user_one == $session_user_id){
$user = $user_two;
} else {
$user = $user_one;
}
$friend_id = getuser($user, 'user_id');
// ----- gives me friends of my friends
$friend_query_two = mysql_query(" SELECT `user_one`, `user_two` FROM `friends` WHERE (`user_one`='$friend_id' and `user_two` != '$session_user_id') OR (`user_one`!='$session_user_id' and `user_two`='$friend_id' ) ");
while($run_friend_two = mysql_fetch_array($friend_query_two)){
$user_one_two = $run_friend_two['user_one'];
$user_two_two = $run_friend_two['user_two'];
if($user_one_two == $friend_id){
$user_two = $user_two_two;
} else {
$user_two = $user_one_two;
}
$friend_id_two = getuser($user_two, 'user_id');
// ------- gives me friends of my friends that are my friends also
$check_friend_query = mysql_query(" SELECT friends_id from friends WHERE (user_one='$session_user_id' AND user_two='$friend_id_two') OR (user_one='$friend_id_two' AND user_two='$session_user_id') ");
if (mysql_num_rows($check_friend_query) != 1){
//here is the problem where I get duplicate values about friends of my friends
$my_friend = $friend_id_two;
$friends_friends = mysql_query("SELECT `user_id`, `name`, `surname`, `email`, `profile` FROM `users` WHERE (`user_id`='$my_friend') ");
while ($run_friends= mysql_fetch_assoc($friends_friends)) {
$friend_user_id = $run_friends['user_id'];
}
?>
我的代码运行良好,给了我朋友的朋友。问题是我得到了一些重复的值。知道如何避免这种情况吗?