PHP:Mysql(“选择ID不在哪里”)
我试图从Mysql_查询中排除an'ID',但它仍然返回提到的ID。 此ID为'21',但查询返回的'21'不是我想要的。 我是否在Mysql中拼错了什么PHP:Mysql(“选择ID不在哪里”),php,mysql,Php,Mysql,我试图从Mysql_查询中排除an'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); $
("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 )
$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());
有几个问题。请看这里:
"SELECT * FROM `gallery` WHERE `gallery_id` NOT IN ('$notgallery')"
$notgallery
是当前要检查的ID数组。您需要使用内爆
将它们重新连接在一起,如下所示:
$notgallery = implode(', ', $id);
此外,您还将库id
的值用引号括起来。所以事实上你会得到这样的结果:
"SELECT * FROM `gallery` WHERE `gallery_id` NOT IN ('21, 13')"
这就像说WHERE gallery\u id!='21、13’
。假设您对id
列使用INT
s,则需要删除$notgallery
周围的单引号。如果您使用的是字符串,则可以更改内爆:
$notgallery = implode("', '", $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());
能否打印\u r
$\u GET['id']
和$notgallery
?请尝试回显查询(而不是结果)。它的内容是什么?您正在传递explode
的结果,这将为您提供一个数组。你需要再次内爆,用逗号作为粘合剂;这意味着您将只匹配整个字符串,而不是单个值<('12,21')中的code>IN与(12,21)中的不同
当某些SQL查询不能满足您的要求时,请始终打印出查询并检查是否一切正常。不会$notgallery=str_replace('.','',',$pic)代码>让方式更有意义?