FIND_IN_SET出错

时间:2012-07-10 08:15:50

标签: php mysql select

我在这个查询中得到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是复选框选中的数组

1 个答案:

答案 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");]

修剪前两个字符。

现在它的工作正常,感谢您的支持。