Reprinted from: thinkphp Summary of the return value of the database operation
Keyword: thinkphp, return value, database operation, select return value, add return value, Setdec return value
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:
Create Operation:
Create () (non-chained), add (), AddAll ()
The supported chain operations are:
Table, data, field, relation, validate, auto, filter, scope, bind, token, comment
Update operation
Save (), SetField (), Setinc (), Setdec ()
The supported chain operations are:
Where, table, alias, field, order, lock, Relation, scope, bind, comment
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
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.
$User =m ("User"), $U 1= $User, $where ("id = 1");//$U 1 can continue to invoke other methods. $result = $U 1->select ();//-------It is generally not necessary to do so, the following code can be implemented $result=m ("User"), $where ("id = 1")->select ();
2. Create operation
Create return value:
If an error occurs, returns false;
Success returns the created data
Create is a non-chained operation and the return value may be a Boolean value, so the operation is strictly judged:
$User = M ("User"), if ($User->create ()) {//For more actions}
Add return value, AddAll return value
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;
An 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
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).
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.
GetField return value
GetField ($field): $field is a field that returns the value of the first row of the field in the database
GetField ($field, True): $field A field that returns a one-dimensional array containing the field
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.
GetField ($fields, ":"): $field as multiple fields, returning an array similar to the one in array ($field 1=> "$field 2: $field 3")
If the query is faulted, the return value of GetField is False
Query success:
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
This article explains the contents of the return value of the database operation in thinkphp, and more relevant content please pay attention to the PHP Chinese web.
Related recommendations:
Related content of thinkphp base class
thinkphp User Login Registration related code case
About database operations for THINKPHP5