我在这个查询中得到phpadmin的结果,但在php(You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
)
select * from luxury_properties LP,property_type PT,cities C
where (FIND_IN_SET('2',LP.luxury_property_feature_id) OR
FIND_IN_SET('7',LP.luxury_property_feature_id) )
AND LP.property_type_id = PT.property_type_id
AND LP.city=C.city_id
AND LP.status=1
order by LP.property_price DESC
其中LP.luxury_property_feature_id
是逗号分隔值
此查询中的问题是什么?
我正在执行这样的
$str='';
if(is_array($luxPropFeatures) && $luxPropFeatures != 0)
{
$selCount = count($luxPropFeatures);
if($selCount >1){
foreach($luxPropFeatures as $val){
$str .= " OR FIND_IN_SET('".$val."',LP.luxury_property_feature_id) ";
$str[0]='';$str[1]='';
}
}else{
foreach($luxPropFeatures as $val) {
$str = " FIND_IN_SET('".$val."',LP.luxury_property_feature_id) ";
}
}
}
$sql = "select * from luxury_properties LP,property_type PT,cities C
WHERE (".$str.")
AND LP.property_type_id = PT.property_type_id
AND LP.city=C.city_id
AND LP.status=1
order by LP.property_price DESC";
其中$luxPropFeatures
是复选框选中的数组
答案 0 :(得分:2)
最后我解决了这个问题,
if($selCount >1){
foreach($luxPropFeatures as $val) {
$str .="OR FIND_IN_SET('".$val."',LP.luxury_property_feature_id) ";
$trimmed = ltrim($str, "OR");
//$str[0]='';$str[2]='';
}
}else{
foreach($luxPropFeatures as $val) {
$trimmed = "FIND_IN_SET('".$val."',LP.luxury_property_feature_id) ";
}
}
之前我试图第一次逃离OR
[OR FIND_IN_SET('2',LP.luxury_property_feature_id) OR FIND_IN_SET('7',LP.luxury_property_feature_id)
]字符,例如$str[0]='';$str[2]='';
我没有设置null,而是使用ltrim [$trimmed = ltrim($str, "OR");
]
现在它的工作正常,感谢您的支持。