简体   繁体   中英

Error in FIND_IN_SET

i am getting result in phpadmin for this query but getting error in 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

Where LP.luxury_property_feature_id is comma separated value

What is the issue in this query?

Updated:

I am executing like this

$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";

Where $luxPropFeatures is checkbox selected array

Finally i fixed the issue,

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) ";
    }

}

Previously i tried to escape the OR [ OR FIND_IN_SET('2',LP.luxury_property_feature_id) OR FIND_IN_SET('7',LP.luxury_property_feature_id) ]character for the first time like $str[0]='';$str[2]='';

Instead of set null, i trimmed the first two char using ltrim [ $trimmed = ltrim($str, "OR"); ]

Now its working fine, thanks for the support.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM