首頁  >  文章  >  後端開發  >  ThinkPHP5查詢資料及處理結果的方法

ThinkPHP5查詢資料及處理結果的方法

不言
不言原創
2018-05-08 11:19:504261瀏覽

這篇文章主要介紹了ThinkPHP5查詢資料及處理結果的方法,結合實例總結分析了thinkPHP5常見查詢語句及查詢資料庫的三種方式,需要的朋友可以參考下

本文實例講述了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. 查詢某筆記錄某個欄位

$device_number_list = model("PackageWhitelist")->where($where)->field("device_number")->find();

$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];
  }
}

再來總結一下TP5的三種查詢資料庫方式

方式一:原生sql查詢

程式碼範例:

<?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(&#39;select * from banner_item where banner_id=?&#39;,[$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(&#39;select * from banner_item where banner_id=?&#39;,[$id]);
//    return $result;
    //2.使用查询构建器
    /*
     * 链式查询Db::table(&#39;banner_item&#39;)->where(&#39;banner_id&#39;,&#39;=&#39;,$id) 返回查询对象,->select();返回查询结果,
     * 除了select操作还有 find(返回一条数据) update delete insert
     * 对应的where 也分三种,1.表达式where(&#39;字段名&#39;,&#39;表达式&#39;,&#39;查询条件&#39;) 2.数组发 3.闭包。
     */
    // 2.1 表达式法
//    $result = Db::table(&#39;banner_item&#39;)
//      ->where(&#39;banner_id&#39;,&#39;=&#39;,$id)
//      ->select();
//    return $result;
    //2.2 闭包法
    $result = Db::table(&#39;banner_item&#39;)
      ->where(function ($query) use($id){
        $query->where(&#39;banner_id&#39;,&#39;=&#39;,$id);
      })
      ->select();
    return $result;
  }
}

方式三:ORM(Object Relation Mapping) 物件關係映射

使用ORM 查詢資料庫主要區別就是在寫模型的繼承think\model類,然後控制器就可以使用model的預設方法來取得資料而不是自己再在模型中專門寫一個獲取方法

程式碼範例:

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 getBannerByID($id){
//    //1.使用原生sql
////    $result = Db::query(&#39;select * from banner_item where banner_id=?&#39;,[$id]);
////    return $result;
//    //2.使用查询构建器
//    /*
//     * 链式查询Db::table(&#39;banner_item&#39;)->where(&#39;banner_id&#39;,&#39;=&#39;,$id) 返回查询对象,->select();返回查询结果,
//     * 除了select操作还有 find(返回一条数据) update delete insert
//     * 对应的where 也分三种,1.表达式where(&#39;字段名&#39;,&#39;表达式&#39;,&#39;查询条件&#39;) 2.数组发 3.闭包。
//     */
//
//    // 2.1 表达式法
////    $result = Db::table(&#39;banner_item&#39;)
////      ->where(&#39;banner_id&#39;,&#39;=&#39;,$id)
////      ->select();
////    return $result;
//    //2.2 闭包法
//    $result = Db::table(&#39;banner_item&#39;)
//      ->where(function ($query) use($id){
//        $query->where(&#39;banner_id&#39;,&#39;=&#39;,$id);
//
//      })
//      ->select();
//    return $result;
//
//
//
//
//
//  }
}

## controller:

<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2017/5/7
 * Time: 下午1:49
 */
namespace app\api\controller\v1;
use app\api\validate\IDMustBePositiveInt;
use app\lib\exception\BannerMissException;
use app\api\model\Banner as BannerModel;
class Banner
{
  public function getBanner($id){
     //调用验证器
    (new IDMustBePositiveInt())->goCheck();
//    $banner = BannerModel::getBannerByID($id);
    $banner = BannerModel::get($id);
    if(!$banner){
      throw new BannerMissException();
    }
    return $banner;
  }
}

相關推薦:

thinkPHP查詢方式小結

########################### #####關於thinkphp查詢以及分頁的問題#########################

以上是ThinkPHP5查詢資料及處理結果的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn