赞
踩
1、根据传过来的in的数组生成预处理占位符
2、拼接普通参数与in参数的处理条件
3、设置数据类型,支持分页查询
- function get_select(){
-
- $params = [':foo'=>'AAA',':bar'=>'bra',':pos'=>0,':lim'=>10];
- $ids = [1,2,3,4,5];
-
- $in = [];
- $in_params =[];
-
- //循环in拼接的数据
- foreach ($ids as $i=>$y){
-
- $key = ":id".$i;
- $in[] = $key;
- $in_params[$key] = $y;
- }
-
- //生成后类似 :id0,:id1,:id2,:id3,:id4 的数据
- $in = implode(',',$in);
-
- //合并数组参数合并后类似得到
- /*
- Array(
- [:name] => AAA
- [:bar] => bra
- [:id0] => 1
- [:id1] => 2
- [:id2] => 3
- [:id3] => 4
- [:id4] => 5
- )
- */
- $map = array_merge($params,$in_params);
-
- $sql = "select * from `table` where `name` = :foo and bar = :bar and id in (".$in.") limit :pos,:lim";
- $sth = $db->prepare($sql);
-
- foreach ($map as $key => $value) {
- $sth->bindValue($key, $value, select_data_type($value));
- }
- $sth->execute();
- $list = $sth->fetchAll(PDO::FETCH_ASSOC);
-
- return $list;
- }
-
- //修改数据类型用于execute limit查询bug
- function select_data_type($val)
- {
- if (is_bool($val)) {
- return PDO::PARAM_BOOL;
- } elseif (is_int($val)) {
- return PDO::PARAM_INT;
- } elseif (is_null($val)) {
- return PDO::PARAM_NULL;
- } else {
- return PDO::PARAM_STR;
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。