5

I am trying to exclude the an 'ID' from the Mysql_query but it is still returning the mentioned ID. This ID is '21' but the query returns '21' which is not what I wanted. Did I misspell something in the 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);
}

I print_r'ed the query and it is still returning '21' which I have excluded/or which I thought I did

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 )
4

3 回答 3

8

有几个问题。看看这里:

"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'。假设您使用INTs 作为id列,您需要删除 . 周围的单引号$notgallery。如果你使用的是字符串,你可以改变你的内爆:

$notgallery = implode("', '", $id);
于 2013-05-02T14:58:36.660 回答
4

$notgallery 是一个数组,在您的 SQL 查询中,您必须有一个用逗号分隔的 id 列表,因此请尝试:

$pic = $_GET['id'];
$id = explode(".", $pic);
$notgallery = $id;
$notgallery = implode(",", $notgallery);
$notg = mysql_query("SELECT * FROM `gallery` WHERE `gallery_id` NOT IN ($notgallery)") or die (mysql_error());
于 2013-05-02T14:55:45.080 回答
2

有上面的帖子 n 更好的方式来放置它。

$pic = $_GET['id'];
$id = explode(".", $pic);
$notgallery = $id;
$notgallery = implode(",", $notgallery);
$notg = mysql_query("SELECT * FROM `gallery` WHERE `gallery_id` NOT IN ($notgallery)") or die (mysql_error());
于 2013-05-02T16:05:43.327 回答