2013-05-02 56 views
4

我想从Mysql_query中排除'ID',但它仍然返回提到的ID。 此ID为'21',但查询返回'21',这不是我想要的。 我在Mysql中拼错了什么?PHP:Mysql(“SELECT WHERE ID NOT”)

("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) 
+0

你可以print_r'$ _GET ['id']'和'$ notgallery'吗? – 2013-05-02 14:52:18

+0

尝试回显查询(不是结果)。它的内容是什么? – 2013-05-02 14:52:58

+0

Array([0] => 21 [1] => jpg)21.jpg – Belzelga 2013-05-02 14:53:35

回答

6

有几个问题。看看这里:

"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 s作为id列,则需要删除围绕$notgallery的单引号。如果您使用的是字符串,你可以改变你的破灭:

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

$ 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()); 
+0

哇!谢谢。它的工作原理 – Belzelga 2013-05-02 14:57:33

+3

不会'$ notgallery = str_replace('。',',',$ pic);'更有意义吗? – 2013-05-02 15:03:10

1

具有上述(POST)更好的方式把它。

$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()); 
相关问题