THINKPHP5 implements a query database and returns a JSON data instance

Source: Internet
Author: User
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 ();  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.