我正在处理带有多个过滤器的搜索表单..但是有意想不到的结果 M使用PDO做它。并基于url创建查询...如果,对于语句
网址:lType=sale&gender=M|F&loc=can|india|asia
SELECT * FROM table WHERE 1=1 AND type like :ltype AND location IN (:loca0) OR location IN (:loca1) OR location IN (:loca2) AND gender IN (:gend0) OR gender IN (:gend1)
所以从url我们可以理解我想得到的结果是type = sale(和)gender = m(或)f和Loc = can(或)idnia(或)asia
但没有按预期获得结果..
...谢谢
更新
由user876345
审判,我试图修改我的循环以绑定位置
if(isset($_GET['loc'])){ //locations
for($i=0;$i<count($aloc);$i++){
if($i<1 && count($aloc)==1) {$sqlsl .=" AND (location IN (:loca$i))";}
else if($i<1 && count($aloc)>1 && count($aloc)!="$i" ) {$sqlsl .=" AND (location IN (:loca$i)";}
else if($i>=1 && count($aloc)>1 && count($aloc)!="$i") { $sqlsl .=" OR location IN (:loca$i)";}
else if($i>1 && count($aloc)>1 && count($aloc)==$i+1) { $sqlsl .=")";}
$bind['loca' . $i] = $aloc[$i];
}
}
但如果超过1个位置并输出
,则此循环不会在末尾添加)
SELECT * FROM table WHERE 1=1 AND (location IN (:loca0) OR location IN (:loca1) OR location IN (:loca2)
答案 0 :(得分:2)
在适当的()
SELECT * FROM table WHERE 1=1 AND (type like :ltype)
AND (location IN (:loca0) OR location IN (:loca1) OR location IN (:loca2))
AND (gender IN (:gend0) OR gender IN (:gend1))
答案 1 :(得分:1)
将您的或/和条件与where子句中的正确括号/分组配对。
SELECT * FROM table WHERE 1=1
AND type like :ltype
AND (location IN (:loca0) OR location IN (:loca1) OR location IN (:loca2) )
AND (gender IN (:gend0) OR gender IN (:gend1))