我有数组例如:
class TimeRangeRouteFilter
{
public function onFilterController(FilterControllerEvent $event) {
$request = $event->getRequest();
$attributes = $request->attributes;
$routeParams = $attributes->get('_route_params');
$end = $routeParams['end'];
$start = $routeParams['start'];
if(!/* in range */) {
throw new NotFoundHttpException();
}
}
}
绑定参数的函数:
$array['city']['val'] = $city; //city value passed to bindParam()
$array['city']['type'] = 'string'; //type passed to bindParam()
$array['city_id']['val'] = $city_id;
$array['city_id']['type'] = 'int';
$query = "update cities set city=:city where city_id=:city_id";
提前致谢
答案 0 :(得分:0)
不,这个解决方案并不好。这很不方便且容易出错 是的,有一个更好的解决方案来运行常规查询:
将数据放在数组中
$array['city'] = $city;
$array['city_id'] = $city_id;
然后将其直接发送到execute()
:
$query = "update cities set city=:city where city_id=:city_id";
$pdo->prepare($query)->execute($array);
所有运行此查询所需的代码,不需要任何笨拙的BindParameters
函数。
只要您将PDO::ATTR_EMULATE_PREPARES
保留为false
,此方法就不会给您带来任何麻烦。
对于常规SQL查询,您永远不需要bindParam()
nor it's third parameter。
所以,将此功能设为此
function run($query, $array = NULL) {
$st = $this->dbc->prepare($query);
$st->execute($array);
return $st;
}