按用户ID排除用户

时间:2014-01-18 12:42:04

标签: php mysql

这是我用于排行榜的代码,但它显示了我想删除的管理员用户

这是我试过的代码

<?
$j = 0;
foreach($tops as $top){
    $j++;
    $user = $db->QueryFetchArray("SELECT id,login,email,country,coins FROM `users` WHERE `id`='".$top['uid']."'");
      if (!in_array($user->user_id, $excluded_users))
    {
        $excluded_id = array(1);
        //...
    }
?>

我尝试删除的用户ID是1

我哪里错了?

3 个答案:

答案 0 :(得分:2)

使用NOT方法,如下所示:

SELECT id,login,email,country,coins 
FROM `users` 
WHERE `id`='".$top['uid']."'
AND `id` <> 1

参考:Comparison Functions and Operators

答案 1 :(得分:2)

使用此查询

SELECT id,login,email,country,coins FROM `users` WHERE `id`='".$top['uid']."' and `id` <>'1'

答案 2 :(得分:2)

<?php
$j = 0;
$excluded_users = array(1);
foreach($tops as $top) {
    if( in_array($top['uid'], $excluded_users ) {
        continue;
    }

    $j++;
    $user = $db->QueryFetchArray("SELECT id,login,email,country,coins FROM `users` WHERE `id`='".$top['uid']."'");

    //...
}
?>

continue允许您跳到循环中的下一个迭代。