我有一个为RSS提要创建的PHP页面。 这拉了一所学校的课程。URL中的多个WHERE语句
在URL中,我添加了locationid(按位置筛选)和course parent(按学校过滤)。
URL结构是目前的index.php?LOCID = 1 & TSID = 2(TS =培训网站) 这个拉已经locationid = 1的所有课程和培训现场ID = 2 运行完美。
的问题是,现在我们需要能够拉从URL两个位置SO LOCID = 1 OR = 2 我想加入另一个变量的(?LOCID = 1 & locid2 = 2),但是我限制到这个查询中的两个位置。即使如果我有足够的智慧来使用圆括号,如果有两个WHERE(locationid = 1 OR locationid = 2)
有没有一种智能的方法来做到这一点?
这是代码。
$where = array();
if (isset($_GET['tsid'])) {
$trainingsite = $_GET['tsid'];
$where[] = 'scheduledcourses.courseparent = '. $trainingsite ."";
}
if (isset($_GET['locid'])) {
$locationid = $_GET['locid'];
$where[] = 'scheduledcourses.courselocationid = '. $locationid ."";
}
if(count($where) >= 0) { array_unshift($where,""); }
$where = implode(" AND ", $where);
$where;
这里是在查询
WHERE coursedate >= CURRENT_DATE() AND privatecourse='no' AND $where";
预先感谢您的WHERE语句!
,如果我正确地理解你的问题,你想在配置参数是数字NUM的基地动态创建MySQL查询。我对么? – 2014-10-02 13:30:27
房间里有一只看不见的大象:SQL注入!大声哭泣,为什么大家都想立即破解OP的网站?至少有一点SQL注入就足够了,例如通过检查所有id是否真的是数字。 – nalply 2014-10-02 13:41:30