There are many ways to read data in thinkphp, usually divided into reading data, reading data sets, and reading field values.
The consistent methods supported by the data query method are:
| Consistent operation |
Role |
Supported parameter types |
| where |
definitions for querying or updating conditions |
strings, arrays, and objects |
| Table |
Used to define the name of the data table to manipulate |
Strings and Arrays |
| Alias |
Used to define aliases for the current data table |
String |
| Field |
Used to define the fields to query (support field exclusions) |
Strings and Arrays |
| Order |
Used to sort the results |
Strings and Arrays |
| Group |
Group support for queries |
String |
| Having |
The having support for queries |
String |
| Join |
Join support for queries |
Strings and Arrays |
| Union |
Union Support for queries |
strings, arrays, and objects |
| Distinct |
Distinct support for queries |
Boolean value |
| Lock |
Lock mechanism for database |
Boolean value |
| Cache |
For query caching |
Supports multiple parameters |
| Relation |
For correlation queries (requires associated model support) |
String |
| Result |
Used to return data transformations |
String |
| Scope |
For named ranges |
String, array |
| Bind |
For data-binding operations |
Array |
| Comment |
For SQL comments |
String |
| Fetchsql |
Do not execute SQL and just return SQL |
Boolean value |
Note: In some cases, some coherent operations are not valid, for example, the limit method is not valid for the Find method.
Reading data
Reading data refers to reading a row of data (or associated data) in a data table, mainly through the Find method, for example:
$User // $data$User->where (' Status=1 and Name= ' thinkphp '), find ();d UMP ($ Data);
The Find method can be used in conjunction with related coherent operations when querying data, the most critical of which is the Where method.
If the query fails, the Find method returns False, and if the query result is NULL, the query returns an associative array (the key value is the field name or alias) if NULL is returned. If the above query succeeds, it will output:
Array (size=3) string ' thinkphp ' (length=8) string ' [email protected] ' (length=18) ' status ' + int 1
The Find method returns only the first record (which can be queried by the order method), even if there is more than one data that satisfies the condition.
You can also get the data object after the query (after the query is successful) by using data method
$User // $User->where (' Status=1 and Name= ' thinkphp '),find ();d UMP ($User ->data ());
Reading data sets
Reading a dataset is actually getting multiple rows of records (and associated data) from the data table, using the Select method, using the example:
$User // instantiate the User object//Find the status value of 1 to create a time sort to return 10 data $list $User->where (' Status=1 ')->order (' Create_time ')->limit ()->select ();
If the query is faulted, the return value of select is False and NULL is returned if the query result is NULL, otherwise a two-dimensional array is returned.
Read field values
The most common way to read a field value is to get multiple or individual data from a column in a data table, the most commonly used method is the GetField method.
Examples are as follows:
$User // $nickname$User->where (' id=3 ')->getfield (' nickname ');
By default, when there is only one field, the value of the first row of the field in the data table that satisfies the criteria is returned.
If you need to return the data for the entire column, you can use:
$User->getfield (' id ',true// Get ID array//Return data format such as array (1,2,3,4,5) one-dimensional arrays, Where value is the value of each row of the ID column
If multiple fields are passed in, an associative array is returned by default:
$User // $list$User->getfield (' id,nickname '); // Two fields are returned with an associative array of array (' id ' = = ' nickname '), with the value of the ID of the Key,nickname field as value
This returns the list is an array, the key name is the user's ID field value, the key value is the user's nickname nickname.
If you pass in the names of multiple fields, for example:
$list $User->getfield (' Id,nickname,email '); // the returned array format is array (' ID ' =>array (' id ' =>value, ' nickname ' =>value, ' email ' =>value) ' is a two-dimensional array,
Key is also the value of the ID field, but value is an array of an entire row, similar to the result traversal of the Select () method, sets the value of the ID to the array key
Returns a two-dimensional array, similar to the return result of the Select method, except that the key name of the two-dimensional array is the user's ID (exactly the first field name of the GetField method).
If we pass in a string delimiter:
$list $User->getfield (' Id,nickname,email ', ': ');
Then the result of the return is an array, the key name is the user ID, and the key value is the output string of Nickname:email.
The GetField method can also support limiting quantities, such as:
$this // limit return 5 records $this // get ID Array limit 3 records
ThinkPHP3.2 Basic Tutorial (21)--model-CURD operation-Data read