Summary of the return value of the database operation in the "Go" thinkphp

Source: Internet
Author: User
Tags addall instance method

The Think\model class in thinkphp provides a basic curd of the database (Create, Update, read, and delete), which can be easily manipulated.
The main methods of model classes and extension classes are:

    1. Create Operation:
      Create () (non-chained), add (), AddAll ()
      The supported chain operations are:
      Table, data, field, relation, validate, auto, filter, scope, bind, token, comment
    2. Update operation
      Save (), SetField (), Setinc (), Setdec ()
      The supported chain operations are:
      Where, table, alias, field, order, lock, Relation, scope, bind, comment
    3. Read operation
      Find (), select (), GetField (), Count, Max, Min, AVG, Sum
      The supported chain operations are:
      Where, table, alias, field, order, group, having, join, union, DISTINCT, lock, cache, relation, result, scope, bind, comment
    4. Delete operation
      Delete ()
      The supported chain operations are:
      Where, table, alias, order, lock, Relation, scope, bind, comment

The possible return values of these methods are false/true/an instance of an array/two-dimensional array/numeric (number)/null/class itself, which is now summarized as follows:

1, chain-operated:

So the chained operation returns an instance of the model class, that is, the return value can be used to continue invoking the instance method.

  1. $User=M("User");
  2. $U1=$User->$where("id = 1");//$U1可以继续调用其他方法。
  3. $result=$U1->select();
  4. //-------一般情况下没有必要这样,下面一句代码即可实现
  5. $result=M("User")->$where("id = 1")->select();
2. Create Operation
    1. Create return value:
      1. returns False if an error occurs,
      2. success returns the created data
        Create is a non-chained operation and the return value is a possible boolean value, so the operation is strictly judged:
        1. $User = M" User "
        2. if ( $User ->create ()) {
        3. //do more
        4. }
    2. Add return value, AddAll return value
      1. success: If the primary key is an autogrow type, the return value is the new record ID value (AddAll is the maximum value), otherwise it returns true;
      2. error has returned false;
3. Update Operation
    • Save return value (SetField, Setinc, Setdec the last call is the Save method, so the return value is consistent with Save):
      The Save return value is the number of records affected (possibly 0), the update error returns false, so be sure to use identity to determine if the update failed.
4. Read Operation
    1. Find return value
      If the query fails, the return value of find is False
      If the query result is null and returns NULL, the query returns an associative array (the key value is the field name or alias).
    2. Select return value
      If the query is faulted, the return value of select is False,
      Returns NULL if the query result is empty, otherwise a two-dimensional array is returned.
    3. GetField return value
      1. If the query is faulted, the return value of GetField is False
      2. Query success:
        1. GetField ($field): $field is a field that returns the value of the first row of the field in the database
        2. GetField ($field, True): $field A field that returns a one-dimensional array containing the field
        3. GetField ($fields): The $fields is two fields (separated by ","), returning an array of arrays ($field 1=> $field 2), and if $field is multiple fields, returns a multidimensional array similar to select, with the key named Field1.
        4. GetField ($fields, ":"): $field as multiple fields, returning an array similar to the one in array ($field 1=> "$field 2: $field 3")
    4. Count, Max, Min, AVG, sum return value
      If the query is faulted, the return value is False
      If the query succeeds, the corresponding value is returned
5. Delete Operation

Delete return Value: Success returns TRUE, query error returns false

Source: http://www.baiwar.com/post/thinkphp-database-operations-in-the-return-value.html

Go "thinkphp database operation return value summary

Related Article

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.