ThinkPHP5 query data and methods of processing results

Source: Internet
Author: User
This article mainly introduced the THINKPHP5 query data and the processing result method, combined with the example summary Analysis thinkPHP5 Common query statement and the query database three kinds of ways, needs friend can refer to the next

This paper describes the THINKPHP5 query data and the method of processing the result. Share to everyone for your reference, as follows:

In the processing of database query results encountered some problems, recorded under the use of several query methods and results processing.

1. Querying a 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. Querying a field for a record

$device _number_list = Model ("Packagewhitelist")->where ($where)->field ("Device_number")->find ();

$this->assign ("Device_number", $device _number_list[' Device_number ']);

3. Querying multiple records for a field and processing the result as a result of 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. Querying 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"): Ten;  $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" and "= $version _name[" Version_name "]) + $package _infos[$key];}  }

Let's summarize the TP5 three ways to query the database

Method one: Native SQL query

code example:

<?php/** * Created by Phpstorm. * User:chenzhitao * DATE:2017/5/8 * Time: PM 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;  }}

Mode two: Using the Query Builder

code example:

<?php/** * Created by Phpstorm. * User:chenzhitao * DATE:2017/5/8 * Time: PM 2:15 */namespace app\api\model;use think\db;use think\exception;class Banner {public  static function Getbannerbyid ($id) {    //1. Using native sql//    $result = db::query (' SELECT * from Banner_item Where banner_id=? ', [$id]);//    return $result;    2. Use the Query Builder    /     * * Chain query db::table (' Banner_item ')->where (' banner_id ', ' = ', $id) to return the query object,->select (); return query results ,     * In addition to the select operation there is a find (return a data) update delete insert     corresponding to the where is also divided into three kinds, 1. Expression where (' field name ', ' expression ', ' query condition ') 2. Array Hair 3. Closures. *///    2.1 expression method//    $result = db::table (' Banner_item ')//      ->where (' banner_id ', ' = ', $id)//      - >select ();//    return $result;    2.2 Closure Method    $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 between using ORM to query a database is to write the model's inherited Think\model class, and then the controller can use the default method of model to get the data instead of writing a fetch method in the model.

code example:

Model

<?php/** * Created by Phpstorm. * User:chenzhitao * DATE:2017/5/8 * Time: 2:15 */namespace app\api\model;use think\db;use think\model;class Banner extends model{//public static function getbannerbyi D ($id) {////1. Using native sql////$result = Db::query (' select * from Banner_item where banner_id=? ', [$id]);////return $r esult;////2. Using the Query Builder///*//* Chain query db::table (' Banner_item ')->where (' banner_id ', ' = ', $id) returns the query object,->select (); Back to the query results,//* In addition to the select operation there is a find (return a data) update delete insert//* Corresponding to the where also divided into three kinds, 1. Expression where (' field name ', ' expression ', ' query condition ') 2. Array Hair 3 . Closures. *///////2.1 expression method////$result = db::table (' Banner_item ')////->where (' banner_id ', ' = ', $id)////- >select ();////return $result;////2.2 closed closure//$result = db::table (' Banner_item ')//->where (function ($q uery) use ($id) {//$query->where (' banner_id ', ' = ', $id);////})//->select ();//return $result;//// ////////  }}

Controller

<?php/** * Created by Phpstorm. * User:chenzhitao * DATE:2017/5/7 * Time: PM 1:49 */namespace app\api\controller\v1;use App\api\validate\idmustbepositiv Eint;use app\lib\exception\bannermissexception;use App\api\model\banner as Bannermodel;class Banner{public  function Getbanner ($id) {     //Call Authenticator    (new Idmustbepositiveint ())->gocheck ();//    $banner = Bannermodel:: Getbannerbyid ($id);    $banner = Bannermodel::get ($id);    if (! $banner) {      throw new bannermissexception ();    }    return $banner;  }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.