Home>Article>PHP Framework> How to query and process data in ThinkPHP5 framework
The followingthinkphpframework tutorial column will introduce to you how to query and process data in the ThinkPHP5 framework. I hope it will be helpful to friends in need!
I encountered some problems when processing database query results. Record the several query methods and result processing used.
1. Query a certain record
$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. Query a certain field of a certain record
$device_number_list = model("PackageWhitelist")->where($where)->field("device_number")->find();
$this->assign("device_number",$device_number_list['device_number']);
3. Query a certain field of multiple records and process the results. It is an array set
$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. Query multiple records
$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. Query in page form and process the results.
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]; } }
Let’s summarize the three ways of querying the database in TP5
Method 1: Native sql query
Code sample:
Method 2: Use query Builder
Code example:
where('banner_id','=',$id) 返回查询对象,->select();返回查询结果, * 除了select操作还有 find(返回一条数据) update delete insert * 对应的where 也分三种,1.表达式where('字段名','表达式','查询条件') 2.数组发 3.闭包。 */ // 2.1 表达式法 // $result = Db::table('banner_item') // ->where('banner_id','=',$id) // ->select(); // return $result; //2.2 闭包法 $result = Db::table('banner_item') ->where(function ($query) use($id){ $query->where('banner_id','=',$id); }) ->select(); return $result; } }Method three: ORM (Object Relation Mapping) Object relational mapping
The main difference in using ORM to query the database is the inheritance of writing model think\ model class, then the controller can use the default method of the model to obtain the data instead of writing a special acquisition method in the model
Code example:
model:
where('banner_id','=',$id) 返回查询对象,->select();返回查询结果, // * 除了select操作还有 find(返回一条数据) update delete insert // * 对应的where 也分三种,1.表达式where('字段名','表达式','查询条件') 2.数组发 3.闭包。 // */ // // // 2.1 表达式法 //// $result = Db::table('banner_item') //// ->where('banner_id','=',$id) //// ->select(); //// return $result; // //2.2 闭包法 // $result = Db::table('banner_item') // ->where(function ($query) use($id){ // $query->where('banner_id','=',$id); // // }) // ->select(); // return $result; // // // // // // } }controller:
goCheck(); // $banner = BannerModel::getBannerByID($id); $banner = BannerModel::get($id); if(!$banner){ throw new BannerMissException(); } return $banner; } }The above is the detailed content of How to query and process data in ThinkPHP5 framework. For more information, please follow other related articles on the PHP Chinese website!