This article mainly introduces common data query methods of thinkPHP, and summarizes and analyzes the specific operation steps and implementation skills of select, find, getField, query, and other methods for database query operations based on the instance form, for more information about thinkPHP data query methods, see the following article: select, find, getField, query and other methods to perform database query operations specific operation steps and related implementation skills, need friends can refer to the next
This document describes common thinkPHP data query methods. We will share this with you for your reference. The details are as follows:
Thinkphp has encapsulated common query methods, which are more practical. the original query methods are also retained for infrequently used query frameworks.query.
$ Model = new Model () // instantiate a model object without corresponding data tables $ Model-> query ("select * from think_user where status = 1 ");
If Thinkphp is not familiar with the framework, you can usequery($sql)Andexecute($sql)The two methods can implement any SQL operation.queryUsed for query operations,executeUsed for non-query operations. However, the framework has encapsulated common methods and is more convenient to use.
The following are the most common query methods:
1. select ()
// Returns false if all data is found. if no data is found, null $ user = M ('demo') is returned; $ data = $ user-> select (); dump ($ data); // add the condition $ user-> field ('name, sex ')-> where ('Id> 2')-> order ('age ') -> limit (3)-> select (); // query information with a primary key value of 30 $ user-> select ('30'); // query a primary key, value of 27: $ user-> select ('21 ');
2. find ()
// Query a piece of data $ user = M ('demo'); // if a failure occurs, falseif ($ data = $ user-> find () is returned ()) {dump ($ data) ;}// add the where Condition $ user = M ('demo'); $ data = $ user-> field ('name, sex ') -> where ('Id> 2')-> find (); dump ($ data); // returns a one-dimensional array $ data-> find ('30 '); $ manager-> where ("username = '$ username' and password =' $ password'")-> find ();
3. getField ()
// Obtain the first $ user = M ('demo') in the column data; $ data = $ user-> getField ('name '); // by default, the first parameter is set to true, and the second parameter is set to true. the entire column of data is obtained. $ user-> where ("id = 3")-> getField ('name', true ); // limit the number of display items $ nickname = $ User-> where ('status = 1')-> getField ('nickname', 8 ); $ nickname = $ User-> where ('status = 1')-> limit (8)-> getField ('nickname', true); // returns a two-dimensional array, key name: First $ nickname = $ User-> where ('status = 1')-> getField ('Id, nickname, sex '); // use the connector ': 'key name is the id value, and key value is the string consisting of account: nickname connection $ result = $ User-> where ('status = 1')-> getField ('Id, account, nickname ',':');
For details about the query method, see the "model> query statement" section in the ThinkPHP3.2 manual.
The above is a detailed summary of the sample code for common thinkPHP data query methods. For more information, see other related articles in the first PHP community!