ThinkPHP5 查询数据并处理结果
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ThinkPHP5 查询数据并处理结果相关的知识,希望对你有一定的参考价值。
在处理数据库查询结果时遇到了些问题,记录下用到过的几种查询方式和结果处理。
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];
- }
- }
以上是关于ThinkPHP5 查询数据并处理结果的主要内容,如果未能解决你的问题,请参考以下文章
thinkphp5一个表里的字段值就是另一个表里的字段值,怎么获取另一个表的字段值呢