本文實例講述了thinkPHP5實現(xiàn)的查詢數(shù)據(jù)庫并返回json數(shù)據(jù)。分享給大家供大家參考,具體如下:
TP5 實現(xiàn)查詢數(shù)據(jù)庫返回json數(shù)據(jù)(返回json數(shù)據(jù)函數(shù)實例)
返回結(jié)果:
一、把格式化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(); } //學(xué)校列表 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è)計有所幫助。
- tp5.1框架數(shù)據(jù)庫子查詢操作實例分析
- tp5.1 框架數(shù)據(jù)庫常見操作詳解【添加、刪除、更新、查詢】
- tp5.1 框架數(shù)據(jù)庫高級查詢技巧實例總結(jié)
- ThinkPHP5.1框架數(shù)據(jù)庫鏈接和增刪改查操作示例
- PHP利用pdo_odbc實現(xiàn)連接數(shù)據(jù)庫示例【基于ThinkPHP5.1搭建的項目】
- PHP7使用ODBC連接SQL Server2008 R2數(shù)據(jù)庫示例【基于thinkPHP5.1框架】
- ThinkPHP實現(xiàn)多數(shù)據(jù)庫連接的解決方法
- tp5(thinkPHP5)框架數(shù)據(jù)庫Db增刪改查常見操作總結(jié)
- tp5(thinkPHP5)框架實現(xiàn)多數(shù)據(jù)庫查詢的方法
- tp5(thinkPHP5)操作mongoDB數(shù)據(jù)庫的方法
- thinkPHP5實現(xiàn)數(shù)據(jù)庫添加內(nèi)容的方法
- tp5.1 框架數(shù)據(jù)庫-數(shù)據(jù)集操作實例分析
標簽:駐馬店 阜陽 欽州 黑河 中衛(wèi) 臨夏 延邊 南昌
巨人網(wǎng)絡(luò)通訊聲明:本文標題《thinkPHP5實現(xiàn)的查詢數(shù)據(jù)庫并返回json數(shù)據(jù)實例》,本文關(guān)鍵詞 thinkPHP5,實現(xiàn),的,查詢,數(shù)據(jù)庫,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。- 惠州外呼調(diào)研系統(tǒng)(外呼調(diào)研客服)
- 本文目錄一覽: 1、深圳外呼體系價格多少錢?2、電銷體系的外呼方法都有哪些?3、外呼體系怎樣處理?4、有哪種比較聞名...
- 11-28