php - FIND_IN_SET 错误

标签 php mysql select

我在 phpadmin 中得到此查询的结果,但在 php 中出现错误(您的 SQL 语法有错误;请检查与您的 MySQL 服务器版本相对应的手册,了解在 '' 附近使用的正确语法第 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是复选框选定的数组

最佳答案

终于解决了这个问题

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"); ] 修剪前两个字符

现在一切正常,感谢您的支持。

关于php - FIND_IN_SET 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11409219/

相关文章:

php - laravel 安装错误 : mbstring not found

php - 如何使用 PDO 准备好的语句使用链接中的 id 号从行获取数据

mysql - 如何在查询时避免笛卡尔积?

mysql - SELECT 中的默认值

python-2.7 - Python(2.7)使用套接字和选择模块进行多重聊天(非阻塞代码)

SQL 查询 : select max of 2 columns of same row

php - "Invalid parameter number: parameter was not defined"插入数据

php - 我可以在 PHP 中混合使用 MySQL API 吗?

MySQL:如何检查每个组的哪些条目的值大于其相应组中的平均值?

MYSQL 如果存在则返回一个值,如果不存在则返回另一个