thinkPHP5實現(xiàn)的查詢數(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ù)實例)
返回結果:
一、把格式化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
更多關于PHP相關內(nèi)容感興趣的讀者可查看本站專題:《PHP中json格式數(shù)據(jù)操作技巧匯總》、《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結》、《ThinkPHP常用方法總結》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術總結》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設計有所幫助。
- tp5.1框架數(shù)據(jù)庫子查詢操作實例分析
- tp5.1 框架數(shù)據(jù)庫常見操作詳解【添加、刪除、更新、查詢】
- tp5.1 框架數(shù)據(jù)庫高級查詢技巧實例總結
- 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增刪改查常見操作總結
- tp5(thinkPHP5)框架實現(xiàn)多數(shù)據(jù)庫查詢的方法
- tp5(thinkPHP5)操作mongoDB數(shù)據(jù)庫的方法
- thinkPHP5實現(xiàn)數(shù)據(jù)庫添加內(nèi)容的方法
- tp5.1 框架數(shù)據(jù)庫-數(shù)據(jù)集操作實例分析
相關文章
深思 PHP 數(shù)組遍歷的差異(array_diff 的實現(xiàn))
深思 PHP 數(shù)組遍歷的差異(array_diff 的實現(xiàn))...2006-06-06
PHP實現(xiàn)將科學計數(shù)法轉(zhuǎn)換為原始數(shù)字字符串的方法
這篇文章主要介紹了PHP實現(xiàn)將科學計數(shù)法轉(zhuǎn)換為原始數(shù)字字符串的方法,通過一個簡單的自定義函數(shù)使用正則替換實現(xiàn)該功能,是非常實用的技巧,需要的朋友可以參考下2014-12-12
解決出現(xiàn)SoapFault (looks like we got no XML document)的問題
下面小編就為大家?guī)硪黄鉀Q出現(xiàn)SoapFault (looks like we got no XML document)的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06

