This article mainly introduces the query database implemented by THINKPHP5 and returns the JSON data function, analyzes the THINKPHP5 database query and JSON format data simple operation skills, the need for friends can refer to the following
This example describes the query database implemented by THINKPHP5 and returns the JSON data. Share to everyone for your reference, as follows:
TP5 implement query database return JSON data (return JSON data function instance)
return Result:
Copy the Code code as follows:
{"Code": 0, "msg": "\u6570\u636e\u8fd4\u56de\u6210\u529f", "count": +, "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 "}"}
First, the formatted JSON function to write to the common file common.php, common.php file path in: application/common.php all file files can be referenced
function json ($code, $msg = "", $count, $data =array ()) { $result =array ( ' code ' = + $code, ' msg ' = + $msg, ' count ' = $count, ' data ' = $data ); Output JSON echo json_encode ($result); Exit;}
Second, the query data control mode main.php
application\admin\controller\main.php
<?phpnamespace 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 (); } School list public function School () { $rs =db (' School ')->select (); $rs 1=json (0, ' Data return success ', $rs); Dump ($rs 1);d ie;//print out the return $this, Fetch ();