选择2列ID,不重复

时间:2016-03-18 19:00:53

标签: php mysql

这是我的代码,我需要扫描消息历史记录,其中表中有2个id。 我的数据库

表:MESSAGE

sender,receiver,message    
1,2,helloooo    
2,1,haiii    
1,3,yoo    
2,3,yaa
1,3,sss
3,1,asd

当前$ _session ['userid'] = 1

$userid = $_SESSION['userid'];
$query = mysql_query("SELECT * FROM message WHERE sender = '$userid' OR receiver = '$userid'");
while($data = mysql_fetch_array($que)){
    if($data['sender'] == $userid ){
         echo $data['receiver']."<br>";
    }else{
         echo $data['sender']."<br>";
    }
}

结果是:

2

2

3

3

3

怎么不重复?

我所需要的只是:

2

3

1 个答案:

答案 0 :(得分:0)

您可以做一些像

这样的事情
$userid = $_SESSION['userid'];
$ids = array(); 
$query = mysql_query("SELECT * FROM message WHERE sender = '$userid' OR receiver = '$userid'");
while($data = mysql_fetch_array($que)){
    if($data['sender'] == $userid ){
         $ids[] = $data['receiver']
    }else{
         $ids[] = $data['sender']
    }
}
$result = array_unique($ids);
foreach ($result as $id) {
    echo $id."<br>";
}

希望它有所帮助:)