The ThinkPHPgetField method obtains the value of a field or the index array of multiple fields.

Source: Internet
Author: User
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:




  1. Public function select (){
  2. Header ("Content-Type: text/html; charset = utf-8 ");

  3. $ Dao = M ("User ");
  4. $ List = $ Dao-> getField ('Username ');
  5. Dump ($ list );
  6. }

The query result returns a separate variable, dump
The output is as follows:

String (5) "admin"

Example 2: Use multiple fields:




  1. Public function select (){
  2. Header ("Content-Type: text/html; charset = utf-8 ");

  3. $ Dao = M ("User ");
  4. $ List = $ Dao-> getField ('uid, username ');
  5. Dump ($ list );
  6. }

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:




  1. Array (5 ){
  2. [1] => string (5) "admin"
  3. [2] => string (6) "Big tribe"
  4. [3] => string (4) "open source tribe"
  5. [4] => string (6) "cms"
  6. [5] => string (6) "PHP"
  7. }

Example 3: Use multiple fields, add query conditions, and use |
Separator:




  1. Public function select (){
  2. Header ("Content-Type: text/html; charset = utf-8 ");

  3. $ Dao = M ("User ");
  4. $ List = $ Dao-> getField ('uid, username, email ', 'uid <4', '| ');
  5. Dump ($ list );
  6. }

Dump prints the following results:




  1. Array (3 ){
  2. [1] => string (21) "admin | phplo@w.cn"
  3. [2] => string (23) "Big tribe | dabuluo@163.com"
  4. [3] => string (19) "open source tribe | kyoo@gmail.com"
  5. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.