Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在MySQL中多次使用空变量?_Php_Mysql_Sql_Mysql Error 1064 - Fatal编程技术网

Php 如何在MySQL中多次使用空变量?

Php 如何在MySQL中多次使用空变量?,php,mysql,sql,mysql-error-1064,Php,Mysql,Sql,Mysql Error 1064,我试图在MySQL语句中检查2个空变量,但我似乎可以得到它的语法非常正确。这是我现在拥有的,它不断地给我一个错误。有谁能告诉我怎么才能做好这件事吗 SELECT threads, userid, username, usergroupid FROM " . TABLE_PREFIX . "user WHERE " . iif(!empty($exuserids), "AND userid NOT IN ($exuserids)") . " " . iif(!

我试图在MySQL语句中检查2个空变量,但我似乎可以得到它的语法非常正确。这是我现在拥有的,它不断地给我一个错误。有谁能告诉我怎么才能做好这件事吗

  SELECT threads, userid, username, usergroupid
    FROM " . TABLE_PREFIX . "user
   WHERE  
    " . iif(!empty($exuserids), "AND userid NOT IN ($exuserids)") . "
    " . iif(!empty($exgroups), "AND usergroupid NOT IN ($exgroups)") . "
ORDER BY threads DESC 
   LIMIT 1
使用:


在指定“AND…”之前需要有WHERE子句,-
1=1
将被优化。这是动态SQL用来简化WHERE子句连接的技巧。

难怪它会给您带来错误。您是否使用PHPkit或其他提供
iif
的工具,因为尽管您没有这样说,但上面的内容是用PHP编写的,
iif
不是该语言的一部分

现在,即使您有了它,为什么不漂亮地编译语句呢?收集数组中的条件,使用AND进行内爆,如果WHERE条件不是空的,则添加一个WHERE条件


最后,在查询中使用变量是SQL注入攻击的一个秘诀。

这就成功了。非常感谢。我今天学到了一些新东西。条件已经在设置数组中了。我只想检查它们是否为空,并且只在它们中包含除0以外的任何内容时运行它们。iif是vbulletin中的一个函数<代码>代码函数iif($expression,$returntrue,$returnfalse=''){return($expression?$returntrue:$returnfalse);}
  SELECT threads, userid, username, usergroupid
    FROM " . TABLE_PREFIX . "user
   WHERE 1 = 1 
    " . iif(!empty($exuserids), "AND userid NOT IN ($exuserids)") . "
    " . iif(!empty($exgroups), "AND usergroupid NOT IN ($exgroups)") . "
ORDER BY threads DESC 
   LIMIT 1