The ThinkPHPgetField method obtains the value of a field or the index array getField () of multiple fields ()
The ThinkPHP getField () method is used to obtain the value of a field or the index array of multiple fields. This method and field method
Different,
Is an independent method without the need to use find or select.
Syntax:
Mixed getField (
String fields, mixed condition, string
Spea)
Parameter description:
The parameters are as follows:
Fields
Condition
Spea
Parameters are described as follows:
Fields: Required. the name of the field to be queried. it can be one or more fields.
Condition: Optional. query condition. it can be a character or array. For more information, see
Select
Method query conditions
Spea: (optional) specifies the data interval when multiple fields are used to generate an associated array. the default value is space.
Instance
When there is only one parameter field, only one record is returned in the query result.
LIMIT 1 condition:
- Public function select (){
- Header ("Content-Type: text/html; charset = utf-8 ");
- $ Dao = M ("User ");
- $ List = $ Dao-> getField ('Username ');
- Dump ($ list );
- }
The query result returns a separate variable, dump
The output is as follows:
String (5) "admin"
Example 2: Use multiple fields:
- Public function select (){
- Header ("Content-Type: text/html; charset = utf-8 ");
- $ Dao = M ("User ");
- $ List = $ Dao-> getField ('uid, username ');
- Dump ($ list );
- }
The query result returns a one-dimensional associated array with a key value of fileds.
The value of the first field in the parameter. the result of dump is:
- Array (5 ){
- [1] => string (5) "admin"
- [2] => string (6) "Big tribe"
- [3] => string (4) "open source tribe"
- [4] => string (6) "cms"
- [5] => string (6) "PHP"
- }
Example 3: Use multiple fields, add query conditions, and use |
Separator:
- Public function select (){
- Header ("Content-Type: text/html; charset = utf-8 ");
- $ Dao = M ("User ");
- $ List = $ Dao-> getField ('uid, username, email ', 'uid <4', '| ');
- Dump ($ list );
- }
Dump prints the following results:
- Array (3 ){
- [1] => string (21) "admin | phplo@w.cn"
- [2] => string (23) "Big tribe | dabuluo@163.com"
- [3] => string (19) "open source tribe | kyoo@gmail.com"
- }