PHP: Mysql ("SELECT WHERE ID NOT ")

标签 php mysql

我试图从 Mysql_query 中排除一个“ID”,但它仍然返回提到的 ID。 此 ID 为“21”,但查询返回“21”,这不是我想要的。 我是不是在 Mysql 中拼错了什么?

("SELECT * FROM `gallery` WHERE `gallery_id` NOT IN ('$notgallery')") or die (mysql_error());


function not_gallery($pic){

$pic = $_GET['id'];
$id = explode(".", $pic);
$notgallery = $id;

$notg = mysql_query("SELECT * FROM `gallery` WHERE `gallery_id` NOT IN ('$notgallery')") or die (mysql_error());
while($not_row = mysql_fetch_assoc($notg)){
    $notgimage[] = array(
        'id' => $not_row['gallery_id'],
        'user' => $not_row['user_id'],
        'name' => $not_row['name'],
        'timestamp' => $not_row['timestamp'],
        'ext' => $not_row['ext'],
        'caption' => $not_row['caption'],

    );
}
print_r($notgimage);
}

我 print_r'ed 查询,它仍然返回我已经排除/或我认为我已经排除的“21”

Array ( [0] => Array ( [id] => 21 [user] => 18 [name] => NotDeojeff [timestamp] => 1367219713 [ext] => jpg [caption] => asd ) [1] => Array ( [id] => 22 [user] => 18 [name] => NotDeojeff [timestamp] => 1367225648 [ext] => jpg [caption] => Ogre magi )

最佳答案

有几个问题。看这里:

"SELECT * FROM `gallery` WHERE `gallery_id` NOT IN ('$notgallery')"

$notgallery 当前是要检查的 ID 数组。您需要使用 implode 将它们重新组合在一起,如下所示:

$notgallery = implode(', ', $id);

此外,您已将 gallery_id 的 NOT IN 值括在引号中。所以实际上你会得到类似的东西:

"SELECT * FROM `gallery` WHERE `gallery_id` NOT IN ('21, 13')"

这就像说 WHERE gallery_id != '21, 13'。假设您正在为 id 列使用 INT,您需要删除 $notgallery 周围的单引号。如果您使用的是字符串,则可以更改内爆:

$notgallery = implode("', '", $id);

关于PHP: Mysql ("SELECT WHERE ID NOT "),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16341159/

相关文章:

mysql - 如何删除这个文件排序?

php - 在各自的列中显示嵌套的数组键值php html

php - Laravel 文件权限错误

php - 排列 - 所有可能的数字集

php - Laravel foreach 在 View 中

PHP cookie 特殊字符

php - 使用 PHP 查询 Mysql 数据库时,我只得到 50 行中的 1 行

php - 在 Yii 中重置 CDbConnection

MYSQL 查询用户是否使用特定优惠券代码下订单

mysql - 如何使用 Node.js 返回 SQL 查询结果并将其存储在 localhost 窗口变量中,而不是终端中?