获取名称列表,如果相同

时间:2014-12-25 13:44:50

标签: php pdo

我希望获得具有注册个人资料的相同IP地址的用户的昵称。 现在我明白我需要制作一个if语句,好像ip_adres count()> 1回显IP地址,但我如何计算等于的IP数。

<?php
$sql = "SELECT ip_adres, nickname2 FROM users";
$alias = $db->prepare($sql);
$alias->execute();

foreach($alias as $a){
    $ip_adres = $a['ip_adres'];
    $nickname = $a['nickname2'];
    if($ip_adres){
        echo "<pre>$nickname has a alias profile! $ip_adres</pre> ";
    }
}
?>

OUPUT

First has a alias profile! 213.93.99.38
Second has a alias profile! 213.93.99.38
Third has a alias profile! 213.93.45.33
Bla has a alias profile! 213.93.45.33
ZZZZ has a alias profile! 213.93

2 个答案:

答案 0 :(得分:2)

只需更改您的查询即可确保结果

select nickname2, count(ip_adres) as cnt from users group by nickname2 having cnt > 1;
祝你好运

答案 1 :(得分:2)

$sql = "SELECT ip_adres, nickname2 FROM users";
$users = $db->prepare($sql);
$users->execute();

$ips = array();
$aliases = array();
foreach($users as $user) {
    if (in_array($user['ip_adres'], $ips))
        $aliases[] = $user['ip_adres'];

    $ips[] = $user['ip_adres'];
}
if (empty($aliases)) {
    echo "Aliases not found";
}
else {
    foreach($users as $user) {
        if (in_array($user['ip_adres'], $aliases)) {
            echo $user['nickname2'] . ' has a alias profile! '. $user['ip_adres'] .'<br/>';
        }
    }
}