这篇文章主要介绍了thinkPHP5实现的查询数据库并返回json数据功能,分析了thinkPHP5数据库查询及json格式数据简单操作技巧,需要的朋友可以参考下
本文实例讲述了thinkPHP5实现的查询数据库并返回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,"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"}]}
一、把格式化json函数写到公共文件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; }
二、查询数据 控制方式 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(); }
相关推荐:
以上是thinkPHP5实现的查询数据库并返回json数据实例的详细内容。更多信息请关注PHP中文网其他相关文章!

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

使用依赖注入(DI)的原因是它促进了代码的松耦合、可测试性和可维护性。1)使用构造函数注入依赖,2)避免使用服务定位器,3)利用依赖注入容器管理依赖,4)通过注入依赖提高测试性,5)避免过度注入依赖,6)考虑DI对性能的影响。

phperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovesponsemetimes.2)优化

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

TOOPTIMIZEPHPAPPLICITIONSFORPERSTORANCE,USECACHING,数据库imization,opcodecaching和SererverConfiguration.1)InlumentCachingWithApcutCutoredSatfetchTimes.2)优化的atabasesbasesebasesebasesbasesbasesbaysbysbyIndexing,BeallancingAndWriteExing

依赖性注射inphpisadesignpatternthatenhancesFlexibility,可检验性和ManiaginabilybyByByByByByExternalDependencEctenceScoupling.itallowsforloosecoupling,EasiererTestingThroughMocking,andModularDesign,andModularDesign,butquirscarecarefulscarefullsstructoringDovairing voavoidOverOver-Inje

PHP性能优化可以通过以下步骤实现:1)在脚本顶部使用require_once或include_once减少文件加载次数;2)使用预处理语句和批处理减少数据库查询次数;3)配置OPcache进行opcode缓存;4)启用并配置PHP-FPM优化进程管理;5)使用CDN分发静态资源;6)使用Xdebug或Blackfire进行代码性能分析;7)选择高效的数据结构如数组;8)编写模块化代码以优化执行。

opcodecachingsimplovesphperforvesphpermance bycachingCompiledCode,reducingServerLoadAndResponSetimes.1)itstorescompiledphpcodeinmemory,bypassingparsingparsingparsingandcompiling.2)useopcachebachebachebachebachebachebachebysettingparametersinphametersinphp.ini,likeememeryconmorysmorysmeryplement.33)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3汉化版
中文版,非常好用

Dreamweaver Mac版
视觉化网页开发工具

禅工作室 13.0.1
功能强大的PHP集成开发环境