我在 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/