Lajur tutorial rangka kerja thinkphp berikut akan memperkenalkan kepada anda cara membuat pertanyaan dan memproses data dalam rangka kerja ThinkPHP5 saya harap ia dapat membantu rakan yang memerlukan.
Saya menghadapi beberapa masalah semasa memproses hasil pertanyaan pangkalan data Rekod beberapa kaedah pertanyaan dan pemprosesan hasil yang digunakan.
1. Tanya rekod tertentu
$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 medan berbilang rekod, dan memproses hasilnya, hasilnya ialah set tatasusunan
$device_number_list = model("PackageWhitelist")->where($where)->field("device_number")->find();
$this->assign("device_number",$device_number_list['device_number']);
$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"]; } }
$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"]; } }
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]; } }
Contoh kod:
kaedah Dua: Gunakan pembina pertanyaan Contoh kod:<?php /** * Created by PhpStorm. * User: chenzhitao * Date: 2017/5/8 * Time: 下午2:15 */ namespace app\api\model; use think\Db; use think\Exception; class Banner { public static function getBannerByID($id){ $result = Db::query('select * from banner_item where banner_id=?',[$id]); return $result; } }
<?php /** * Created by PhpStorm. * User: chenzhitao * Date: 2017/5/8 * Time: 下午2:15 */ namespace app\api\model; use think\Db; use think\Exception; class Banner { public static function getBannerByID($id){ //1.使用原生sql // $result = Db::query('select * from banner_item where banner_id=?',[$id]); // return $result; //2.使用查询构建器 /* * 链式查询Db::table('banner_item')->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; } }
Contoh kod:
model:
pengawal:Atas ialah kandungan terperinci Cara untuk bertanya dan memproses data dalam rangka kerja ThinkPHP5. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!