避免在php中重复值

时间:2013-09-04 14:33:51

标签: php mysql

我在php中创建了一个“你可能知道的人”脚本。这个脚本在php朋友系统中向我展示了我朋友的朋友。 我有一个名为 users (user_id, name, surname, email, profile) 的表,其中包含有关用户的信息。另一个名为 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'];
        }
?>

我的代码很好,并且给了我朋友的朋友。问题是我得到了一些重复的值。知道怎么避免这个吗?

7 个答案:

答案 0 :(得分:0)

我认为你会为朋友们提供一个“多对多”的桌子

像:

Table Friends

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

然后你可以"SELECT Friend FROM Friends WHERE user = $user";

答案 1 :(得分:0)

您可以重新构建查询以使用UNION SELECT DISTINCT,并且只为每个子查询检索一个用户ID列。

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

答案 2 :(得分: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);

答案 3 :(得分:0)

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

$variable = mysql_fetch_array($result);

然后尝试removing duplicate values from that array

答案 4 :(得分:0)

最简单的方法是使用array_unique

$run_friends = array_unique($runfriends);
$friend_user_id = $run_friends['user_id'];

答案 5 :(得分: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进行数据库连接

答案 6 :(得分: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