TP5实现查询数据库返回json数据(返回json数据函数实例)TP5 实现查询数据库返回json数据(返回json数据函数实例)
返回结果:
{"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,"u
⼀、把格式化json函数写到公共⽂件common.php中,common.php⽂件路径在:application/common.php 所有⽂件⽂件都可以引⽤
function json($code,$msg="",$count,$data=array()){
phpjson格式化输出$result=array(
'code'=>$code,
'msg'=>$msg,
'count'=>$count,
'data'=>$data
);
//输出json
echo json_encode($result);
exit;
}
⼆、查询数据  控制⽅式 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,'数据返回成功',1000,$rs);
dump($rs1);die;//打印出来
return $this -> fetch();
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。