我在我的一个类中有以下代码部分:
$l = new Location();
$result = $l->getLocIdsbyCity($city); // returns csv
$ids = explode(',', $result);
$where = 'LOC_ID = ' . $ids[0];
unset($ids[0]);
foreach ($ids as $id) {
$where .= ' OR LOC_ID = ' . $id;
}
$select->where($where);有没有更“优雅”的方式来构建select stmt?我需要所有具有所提供I之一的记录..
发布于 2010-11-15 20:21:13
假设您的csv是注入安全的(包含受信任的值,没有用户提供的输入):
$l = new Location();
$result = $l->getLocIdsbyCity($city); // returns csv
$where = "LOC_ID IN ($result)";
$select->where($where);如果不是,你应该分解它,mysql_real_escape_string每个值,然后内爆回来。
发布于 2010-11-15 20:22:28
您可以使用in运算符来形成如下条件:
where LOC_ID in (1,2,3,4,5)如果你确信$result不会包含任何有害的东西,你应该能够直接使用它,而不必拆分和循环。
https://stackoverflow.com/questions/4184218
复制相似问题