Many ThinkPHP reading data in the embodiment, the read data is usually divided into read data and the read set field values.
Stepper motors and servo motors
Data query method supported by consistent methods of operation are:
Coherent operation | effect | Supported parameter types |
---|---|---|
where | Define a query or update the conditions of | Strings, arrays and objects |
table | Defines the name of a data table to be operated | Strings and arrays |
alias | The current data table is used to define an alias | String |
field | Is used to define the fields you want to query (support field excluded) | Strings and arrays |
order | It used to sort the results | Strings and arrays |
group | For group support for query | String |
having | Support for having the query | String |
join | Support for join queries | Strings and arrays |
union | For union support for queries | Strings, arrays and objects |
distinct | Support for distinct queries | Boolean value |
lock | For the locking mechanism of the database | Boolean value |
cache | For query cache | Support for multiple parameters |
relation | For related queries (correlation model needs support) | String |
result | Returning data conversion | String |
scope | For a named range | Strings, arrays |
bind | Data for the binding operation | Array |
comment | For SQL comment | String |
fetchSql | SQL is not executed but only to return SQL | Boolean value |
Note: In some cases some coherent operation is invalid, for example, to find a method limit is not valid.
Read data
It refers to the read data line data (or the related data) read the data in the table, mainly through the find
method completes, for example:
$User = M("User"); // 实例化User对象
// 查找status值为1name值为think的用户数据
$data = $User->where('status=1 AND name="thinkphp"')->find();
dump($data);
Time to find a method to query data can be associated with a consistent method of operation, which is where the most critical method, how to use a method where we will describe in detail the query language section.
If the query error, find the method returns false, if the query result is empty return NULL, the query is successful an associative array is returned (key field names or aliases). If the above query is successful, it will output:
array (size=3)
'name' => string 'thinkphp' (length=8)
'email' => string '[email protected]' (length=18)
'status'=> int 1
Even if more than one satisfy the condition data, find the method will only return the first record (order by the method of sorting the query).
(After the query is successful) may also be obtained after the data objects with data query method
$User = M("User"); // 实例化User对象
// 查找status值为1name值为think的用户数据
$User->where('status=1 AND name="thinkphp"')->find();
dump($User->data());
Reading the data set
Reading the data set actually acquired multiple rows (and associated data) in the data table, the use of select
the method, using the example:
$User = M("User"); // 实例化User对象
// 查找status值为1的用户数据 以创建时间排序 返回10条数据
$list = $User->where('status=1')->order('create_time')->limit(10)->select();
If the query error, select the return value is false, if the query result is empty, NULL is returned, otherwise two-dimensional array.
Read field value
Read field value actually acquiring a plurality of data or a single column in the data table, the most commonly used method is getField
a method.
Examples are as follows:
$User = M("User"); // 实例化User对象
// 获取ID为3的用户的昵称
$nickname = $User->where('id=3')->getField('nickname');
By default, when only one field, returns the value of the first row of the data table of the conditions in the field.
If you need to return the entire column of data, you can use:
$User->getField('id',true); // 获取id数组
//返回数据格式如array(1,2,3,4,5)一维数组,其中value就是id列的每行的值
If you pass more than one field, then default returns an associative array:
$User = M("User"); // 实例化User对象
// 获取所有用户的ID和昵称列表
$list = $User->getField('id,nickname');
//两个字段的情况下返回的是array(`id`=>`nickname`)的关联数组,以id的值为key,nickname字段值为value
Such a list is an array of return, the value of the id field is a key name of the user, the key is the user's nickname nickname.
If the name of the incoming multiple fields, such as:
$list = $User->getField('id,nickname,email');
//返回的数组格式是array(`id`=>array(`id`=>value,`nickname`=>value,`email`=>value))是一个二维数组,key还是id字段的值,但value是整行的array数组,类似于select()方法的结果遍历将id的值设为数组key
Returns a two-dimensional array, similar to the select method returns a result, the key difference is that this is the name of two-dimensional array of user id (accurate to say that the first field name getField method).
If we pass a string delimiter:
$list = $User->getField('id,nickname,email',':');
The result returned is an array, the keys are user id, the key is nickname:email
the output string.
getField method may further limit the number of supports, for example:
$this->getField('id,name',5); // 限制返回5条记录
$this->getField('id',3); // 获取id数组 限制3条记录
It can be used in conjunction with order method. More query methods can query language reference section.