在处理数据库查询结果时遇到了些问题,记录下用到过的几种查询方式和结果处理。
1. 查询某条记录
- $where=array(
- "version_id"=>$version_id
- );
- $data = model("PackageWhitelist")->where($where)->find();
- $this->assign("package_id",$package_id);
- $where=array(
- "package_id"=>$package_id
- );
- $data = model("Package")->where($where)->find();
- if($data){
- $this->assign("target_version",$data[‘target_version‘]);
- }
2. 查询某条记录某个字段
- $this->assign("device_number",$device_number_list[‘device_number‘]);
3. 查询多个记录某个字段 , 并处理结果,结果是一个数组集
- $where=array(
- "version_id"=>$version_id
- );
- $data = model("PackageWhitelist")->where($where)->field("device_number")->select();
- $device_number_list=‘‘;
- foreach($data as $val){
- $list = $val->toArray();
- if($device_number_list){
- $device_number_list=$device_number_list.‘;‘.$list["device_number"];
- }else{
- $device_number_list=$list["device_number"];
- }
- }
4. 查询多条记录
- $where=array(
- "version_id"=>$version_id
- );
- $data = model("PackageWhitelist")->where($where)->select();
- $device_number_list=‘‘;
- foreach($data as $val){
- $list = $val->toArray();
- if($device_number_list){
- $device_number_list=$device_number_list.‘;‘.$list["device_number"];
- }else{
- $device_number_list=$list["device_number"];
- }
- }
5. 已页形式查询,并处理结果。
- public function index($version_id){
- $where=array(
- "version_id"=>$version_id
- );
- $version_name = model("Version")->where($where)->field("version_name")->find();
- $listrows=config("LISTROWS")?config("LISTROWS"):10;
- $package_lists=model("Package")->where($where)->paginate($listrows);
- $package_infos = $package_lists->toArray()["data"];
- foreach($package_infos as $key=>$value){
- $package_infos[$key] = array("source_version" => $version_name["version_name"]) + $package_infos[$key];
- }
- }
时间: 2024-10-27 02:59:12