我试图从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);
}
我打印了查询,它仍然返回'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 )
答案 0 :(得分:7)
有几个问题。看看这里:
"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'
。假设您在INT
列中使用id
,则需要删除$notgallery
周围的单引号。如果您使用的是字符串,则可以更改内爆:
$notgallery = implode("', '", $id);
答案 1 :(得分: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());
答案 2 :(得分:2)
以上是更好的方式。
$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());