0

我在 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'];
        }
?>

我的代码运行良好,给了我朋友的朋友。问题是我得到了一些重复的值。知道如何避免这种情况吗?

4

7 回答 7

0

我认为您最好为朋友使用“多对多”表

喜欢:

Table Friends

user Friend
  0    11
  0    58
  0    87
  1    35
  2    44
  2    98

那么你也能"SELECT Friend FROM Friends WHERE user = $user";

于 2013-09-04T14:42:19.017 回答
0

您可以重组查询以使用UNIONwithSELECT DISTINCT并且只为每个子查询检索一个用户 id 列。

或者,您可以将$user_twoids 放在一个数组中并使用 PHP 的array_uniquefunction。然后遍历这个新数组并获取用户详细信息或仅使用WHERE user_id IN (id1, id2, id3 ...).

于 2013-09-04T14:49:51.470 回答
0

不是更好的解决方案,但您可以使用:

.....

$friends_array = array();

while ($run_friends= mysql_fetch_assoc($friends_friends)) {

    $friend_user_id = $run_friends['user_id'];

    if(!in_array($friend_user_id, $friends_array)) {

        $friends_array[] = $friend_user_id;

    }
}

print_r($friends_array);
于 2013-09-04T14:49:52.753 回答
0

您也可以尝试获取数组中的所有记录,例如

$variable = mysql_fetch_array($result);

然后尝试从该数组中删除重复值

于 2013-09-04T14:50:57.570 回答
0

最简单的方法是使用array_unique

$run_friends = array_unique($runfriends);
$friend_user_id = $run_friends['user_id'];
于 2013-09-04T14:52:27.017 回答
0

这是未经测试的,部分是 sudo 代码,但我会采用这种方法来避免在循环中运行查询

(理论应该是合理的)

<?php
//load your friends
$friends = (SELECT `user_one`, `user_two` FROM `friends` WHERE `user_one`='$session_user_id' OR `user_two`='$session_user_id')

//build array of your friends id's
$friendsOfMine = array()
for ($i = 0, $max = count($friends); $i < $max; $i++) {
    $friendsOfMine[] = ($friends[$i]['user_one'] == $session_user_id) ? 
        $friends[$i]['user_two'] : 
        $friends[$i]['user_one'];
}

$mightKnow = array();
//load your frineds friends
$frOfFr = (SELECT `user_one`, `user_two` FROM `friends` WHERE `user_one` IN(implode(',', $friendsOfMine)) OR `user_two` IN(implode(',', $friendsOfMine)))

//build array of friends of friends id's
for ($i = 0, $max = count($frOfFr); $i < $max; $i++) {
    $mightKnow[] = (in_array(frOfFr[$i]['user_one'])) ? 
        $mightKnow[$i]['user_two'] : 
        $mightKnow[$i]['user_one'];
}

//load users that are in the array of frineds of friends but are not you
$friendsOfFriends = ("SELECT `user_id`, `name`, `surname`, `email`, `profile` FROM `users` WHERE `user_id` IN(" . implode(',',$mightKnow) . ") AND `user_id` NOT `user_id` IN(" . implode(',',$friendsOfMine) . ") AND `user_id` != '" . $session_user_id . "' ")
?>

另一方面,我非常建议您使用 PDO 进行数据库连接

于 2013-09-04T14:55:16.927 回答
0

更新

PHP 实现可能看起来像这样:

global $db;
$db = new PDO('mysql:host=localhost;dbname=<SOMEDB>', '<USERNAME>', 'PASSWORD');

$friends = getFriendsOf( $session_user_id );
$friends_of_friends = getFriendsOf( $friends );

// Do whatever you want right here
// with your friends ($friends)
// and your friends' friends ($friends_of_friends)

function getFriendsOf( $user ) {
    global $db;

    if ( is_array( $user ) ) $where = ' IN (' . implode( ',', $user ) . ')';
    else $where = ' = ' . $user;

    $sql = "
SELECT GROUP_CONCAT( f.user_id ) as friends
  FROM (SELECT user_one as user_id
          FROM friends
         WHERE user_two {$where}
         UNION DISTINCT
        SELECT user_two
          FROM friends
         WHERE user_one {$where}
       ) f";

    $results = $db->query( $sql );
    $row = $results->fetch();
    return explode( ',', $row[ 'friends' ] );
}

原来的

没有任何数据可供测试。但是这样的事情应该可以解决问题。

交友:

SELECT u.*
  FROM (SELECT user_one as user_id
          FROM friends
         WHERE user_two = '$session_user_id'
         UNION DISTINCT
        SELECT user_two
          FROM friends
         WHERE user_one = '$session_user_id'
       ) f
  JOIN users u
    ON u.user_id = f.user_id

获得朋友的朋友:

SELECT u.*
  FROM (SELECT DISTINCT user_one as user_id
          FROM friends
         WHERE user_two IN (SELECT user_one as user_id
                              FROM friends
                             WHERE user_two = '$session_user_id'
                             UNION DISTINCT
                            SELECT user_two
                              FROM friends
                             WHERE user_one = '$session_user_id'
                           )
         UNION DISTINCT
        SELECT DISTINCT user_two
          FROM friends
         WHERE user_one IN (SELECT user_one as user_id
                              FROM friends
                             WHERE user_two = '$session_user_id'
                             UNION DISTINCT
                            SELECT user_two
                              FROM friends
                             WHERE user_one = '$session_user_id'
                           )
       ) f
  JOIN users u
    ON u.user_id = f.user_id
于 2013-09-04T15:12:08.220 回答