Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of return value of Database Operation in ThinkPHP

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "the method of database operation return value in ThinkPHP". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Keywords: Thinkphp, return value, database operation, select return value, add return value, setDec return value

The Think\ Model class in Thinkphp provides the basic CURD of the database (Create, Update, Read, and Delete), through which you can easily operate.

The main methods of the Model class and extension classes are:

Create operation:

Create () (not chained), add (), addAll ()

The chain operations supported are:

Table 、 data 、 field 、 relation 、 validate 、 auto 、 filter 、 scope 、 bind 、 token 、 comment

Update operation

Save (), setField (), setInc (), setDec ()

The chain operations supported are:

Where 、 table 、 alias 、 field 、 order 、 lock 、 relation 、 scope 、 bind 、 comment

Read operation

Find (), select (), getField (), Count, Max, Min, Avg, Sum

The chain operations supported are:

Where 、 table 、 alias 、 field 、 order 、 group 、 having 、 join 、 union 、 distinct 、 lock 、 cache 、 relation 、 result 、 scope 、 bind 、 comment

Delete operation

Delete ()

The chain operations supported are:

Where 、 table 、 alias 、 order 、 lock 、 relation 、 scope 、 bind 、 comment

The possible return values of these methods are instances of the false/true/ one-digit array / two-dimensional array / numeric (numbers) / null/ class itself, which are now summarized as follows:

1. Chain operation:

So all chained operations return an instance of the Model class, which means you can use the return value to continue calling the instance method.

$User=M ("User")

$U1 updated user-> $where ("id = 1"); / / $U1 can continue to call other methods.

$result=$U1- > select ()

/ /-in general, this is not necessary, it can be achieved by the following sentence of code

$result=M ("User")-> $where ("id = 1")-> select ()

2. Create operation

Return value of create:

$User = M ("User")

If ($User- > create ()) {

/ / do more operations

}

If an error occurs, return false

If you succeed, the created data will be returned.

Create is a non-chained operation, and the return value may be a Boolean value, so strict judgment should be made during the operation:

Add return value, addAll return value

Success: if the primary key is auto-growing, the return value is the newly added record ID (addAll is the maximum), otherwise true is returned.

Return false if an error occurs

3. Update operation

Save return value (setField, setInc, and setDec all call the save method last, so the return value is the same as save):

The save return value is the number of records affected (possibly 0), and the update error returns false, so be sure to use identity to determine whether the update failed.

4. Read operation

Find return value

If an error occurs in the query, the return value of find is false

If the query result is empty and returns NULL, the query succeeds and returns an associative array (the key value is the field name or alias).

Select return value

If an error occurs in the query, the return value of select is false

Returns NULL if the query result is empty, otherwise returns a two-dimensional array.

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 is a field that returns an one-dimensional array containing the field

GetField ($fields): $fields is an array of two fields (separated by ",") and returns an array of array ($field1= > $field2); if $field is multiple fields, a multidimensional array similar to select is returned with the key name field1.

GetField ($fields, ":"): $field is a number of fields that return an array like array ($field1= > "$field2:$field3")

If an error occurs in the query, the return value of getField is false

Query successful:

Return values for Count, Max, Min, Avg, Sum

If there is an error in the query, the return value is false

If the query is successful, the corresponding value is returned.

5. Delete operation

Return value of delete: true for success and false for query error

This is the end of the content of "the method of database operation return value in ThinkPHP". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report