


Query database and return json data instance implemented by thinkPHP5
This article mainly introduces the function of querying the database and returning json data implemented by thinkPHP5, and analyzes the simple operation skills of thinkPHP5 database query and json format data. Friends in need can refer to it
This article describes the example of thinkPHP5 The implementation queries the database and returns json data. Share it with everyone for your reference, the details are as follows:
TP5 implements querying the database and returning json data (returning json data function instance)
Return results:
Copy code The code is as follows:
{ "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"}]}
1. Write the formatted json function to the public file common.php. The common.php file path is: application/common.php. All files can be referenced.
function json($code,$msg="",$count,$data=array()){ $result=array( 'code'=>$code, 'msg'=>$msg, 'count'=>$count, 'data'=>$data ); //输出json echo json_encode($result); exit; }
2. Query data Control method 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(); }
Related recommendations:
Introduction to the method of implementing paging function in thinkphp5
The above is the detailed content of Query database and return json data instance implemented by thinkPHP5. For more information, please follow other related articles on the PHP Chinese website!

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment
