亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

thinkPHP5實現(xiàn)的查詢數(shù)據(jù)庫并返回json數(shù)據(jù)實例

 更新時間:2017年10月23日 11:34:14   作者:我是高手高手高高手  
這篇文章主要介紹了thinkPHP5實現(xiàn)的查詢數(shù)據(jù)庫并返回json數(shù)據(jù)功能,結(jié)合實例形式分析了thinkPHP5數(shù)據(jù)庫查詢及json格式數(shù)據(jù)簡單操作技巧,需要的朋友可以參考下

本文實例講述了thinkPHP5實現(xiàn)的查詢數(shù)據(jù)庫并返回json數(shù)據(jù)。分享給大家供大家參考,具體如下:

TP5 實現(xiàn)查詢數(shù)據(jù)庫返回json數(shù)據(jù)(返回json數(shù)據(jù)函數(shù)實例)

返回結(jié)果:

復制代碼 代碼如下:
{"code":0,"msg":"\u6570\u636e\u8fd4\u56de\u6210\u529f","count":1000,"data":[{"id":617,"title":"\u5317\u4eac\u7406\u5de5\u5927\u5b66","flid":1,"pid":0,"uid":1,"price":0,"admin_name":null,"time":"2017-09-22 16:17:16"},{"id":618,"title":"\u5357\u5f00\u5927\u5b66","flid":1,"pid":0,"uid":1,"price":0,"admin_name":null,"time":"2017-09-22 16:17:28"}]}

一、把格式化json函數(shù)寫到公共文件common.php中,common.php文件路徑在:application/common.php 所有文件文件都可以引用

function json($code,$msg="",$count,$data=array()){
  $result=array(
   'code'=>$code,
   'msg'=>$msg,
   'count'=>$count,
   'data'=>$data
  );
  //輸出json
  echo json_encode($result);
  exit;
}

二、查詢數(shù)據(jù)  控制方式 Main.php

application\admin\controller\Main.php

<?php
namespace app\admin\controller;
use think\Controller;
use think\Validate;
use think\Request;
//use think\Db;
class Main extends controller
{
  public function index()
  {
    return $this -> fetch();
  }
//學校列表
  public function school()
  {
    $rs=db('school')->select();
    $rs1=json(0,'數(shù)據(jù)返回成功',1000,$rs);
    dump($rs1);die;//打印出來
    return $this -> fetch();
  }

PS:這里再為大家推薦幾款比較實用的json在線工具供大家參考使用:

在線JSON代碼檢驗、檢驗、美化、格式化工具:
http://tools.jb51.net/code/json

JSON在線格式化工具:
http://tools.jb51.net/code/jsonformat

在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson

json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP中json格式數(shù)據(jù)操作技巧匯總》、《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。

希望本文所述對大家基于ThinkPHP框架的PHP程序設(shè)計有所幫助。

相關(guān)文章

最新評論