在我的搜索表单中,有很多复选框和字段
一些复选框属于该组
示例组,城市。
查询如下
SELECT id, city_id, area, city FROM an_objects
WHERE livedays > 0 AND type_id = :typeoffer AND rubric_id = :typerelaty
AND CASE
WHEN :1r = '' THEN true
ELSE city_id IN (:1r, :2r, :99r, :100r)
END
GROUP BY id ORDER BY date ASC
在这个例子中,需要填充4个参数,或者一百个 但我想做
SELECT id, city_id, area, city FROM an_objects
WHERE livedays > 0 AND type_id = :typeoffer AND rubric_id = :typerelaty
AND CASE
WHEN :1r = '' THEN true
ELSE city_id IN (:arrCity)
END
GROUP BY id ORDER BY date ASC
字符串形成如此
if(isset($param['city']))
{
for($i=0; $i < 9; $i++)
{
if(isset($param['city'][$i]))
$raion .= $param['city'][$i] . ",";
else
break;
}
$arrCity = substr($city, 0, -1);
}
我们有以下
(:arrCity)取代(&#34; 1,2,3,4,5,6&#34;)。
这是作为单个字符串获得的,但如何执行以下操作
(:arrCity)(1,2,3,4,5,6)
答案 0 :(得分:1)
这是一个有趣的问题,它清楚地表明PDO比旧的mysql_ *更难以使用,并且不能为广泛的现实生活查询提供良好的安全性。
为了使这些任务更容易,数据库访问库应该允许两件大事:
和PDO都失败了。
所以,程序员必须自己照顾他们。
因此,通过使用实现这些原则的库,代码既简短又安全:
if(isset($param['city']))
{
$raion = $db->parse("AND (city_id IN (?a)", $param['city']);
}
// you can add any number of such conditions making your search flexible
// the only thing you have to keep in mind: add a value ONLY via placeholder
$sql = "SELECT id, city_id, area, city FROM an_objects
WHERE livedays > 0 AND type_id = ?i AND rubric_id = ?i
?p
GROUP BY id ORDER BY date ASC";
$data = $db->getAll($sql, $typeoffer, $typerelaty, $raion);
是的,这几行简洁易懂的代码!
如果我误解了您的情况 - 请随时询问更多详细信息,提供构建查询的确切逻辑。我很乐意根据你的条件编写确切的代码,只是为了展示我所谈论的图书馆的力量。