Introduction to thinkphp CURD and query methods

Source: Internet
Author: User

Read data
Copy codeThe Code is as follows:
$ M = new Model ('user ');

$ M = M ('user ');

Select

$ M-> select (); // obtain all data and return it as an array

Find

$ M-> find ($ id); // obtain a single data record

GetField (field name) // obtain a specific field value

$ Arr = $ m-> where ('Id = 2')-> getField ('username ');

Iii. ThinkPHP 3 create data (important)

Create
Copy codeThe Code is as follows:
$ M = new Model ('user ');

$ M = M ('user ');

$ M-> Field name = Value

$ M-> add ();

The returned value is the new ID number.

Iv. ThinkPHP 3 Delete data (important)
Copy codeThe Code is as follows:
$ M = M ('user ');

$ M-> delete (2); // delete data with id 2

$ M-> where ('Id = 2')-> delete (); // the data with the id of 2 is also deleted.

The returned value is the number of affected rows.

5. ThinkPHP 3 update data (important)
Copy codeThe Code is as follows:
$ M = M ('user ');

$ Data ['id'] = 1;

$ Data ['username'] = 'ztz2 ';

$ M-> save ($ data );

The returned value is the number of affected rows.

========================================================== ====

I. Common Query Method

Ii. Expression Query Method

Iii. interval Query

Iv. Statistical Query

V. Direct SQL query

I. Common Query Method

A. String
Copy codeThe Code is as follows:
$ Arr = $ m-> where ("sex = 0 and username = 'give'")-> find ();

B. Array
Copy codeThe Code is as follows:
$ Data ['sex'] = 0;

$ Data ['username'] = 'gege ';

$ Arr = $ m-> where ($ data)-> find ();

Note: This method defaults to the relationship between and. If you use the or relationship, you need to add an array value.
Copy codeThe Code is as follows:
$ Data ['sex'] = 0;

$ Data ['username'] = 'gege ';

$ Data ['_ logic'] = 'or ';

Ii. Expression Query Method
Copy codeThe Code is as follows:
$ Data ['id'] = array ('lt ', 6 );

$ Arr = $ m-> where ($ data)-> select ();

Equal to EQ

NEQ is not equal

GT>

EGT greater than or equal

LT is less

ELT less than or equal

LIKE fuzzy query
Copy codeThe Code is as follows:
$ Data ['username'] = array ('like', '% Ge ');

$ Arr = $ m-> where ($ data)-> select ();

NOTLIKE

$ Data ['username'] = array ('notlike', '% ge %'); // there is no space in the notlike Center

$ Arr = $ m-> where ($ data)-> select ();

 

Note: If a field matches multiple wildcards
Copy codeThe Code is as follows:
$ Data ['username'] = array ('like', array ('% ge %', '% 100',' % 5% '),' and '); // if there is no third value, the default relationship is or.

$ Arr = $ m-> where ($ data)-> select ();

BETWEEN

$ Data ['id'] = array ('between', array (5, 7 ));

$ Arr = $ m-> where ($ data)-> select ();

// SELECT * FROM 'tp _ user' WHERE ('id' BETWEEN 5 AND 7 ))

$ Data ['id'] = array ('not between', array (5, 7); // note that spaces must exist between not and.

$ Arr = $ m-> where ($ data)-> select ();

IN

$ Data ['id'] = array ('in', array (4,6, 7 ));

$ Arr = $ m-> where ($ data)-> select ();

// SELECT * FROM 'tp _ user' WHERE ('id' IN (4,6, 7 ))

$ Data ['id'] = array ('not in', array (, 7 ));

$ Arr = $ m-> where ($ data)-> select ();

// SELECT * FROM 'tp _ user' WHERE ('id' not in (4,6, 7 ))

Iii. interval Query
Copy codeThe Code is as follows:
$ Data ['id'] = array ('gt ', 4), array ('lt', 10); // The default relationship is the relationship between and.

// SELECT * FROM 'tp _ user' WHERE ('id'> 4) AND ('id' <10 ))

$ Data ['id'] = array ('gt ', 4), array ('lt', 10), or ') // The relationship is the or relationship.

$ Data ['name'] = array ('like', '% 100'), array ('like',' % 5% '), 'gege ', 'or ');

Iv. Statistical Query

Count // obtain the number

Max // obtain the maximum number

Min // obtain the minimum number

Avg // obtain the average

Sum // get the sum

V. Direct SQL query

A. query mainly processes the read data

Result set of successfully returned data

Boolean false is returned for failure.
Copy codeThe Code is as follows:
$ M = M ();

$ Result = $ m-> query ("select * from t_user where id> 50 ");

Var_dump ($ result );

B. execute is used to update write operations.

Number of affected rows returned successfully

Boolean false is returned for failure.
Copy codeThe Code is as follows:
$ M = M ();

$ Result = $ m-> execute ("insert into t_user ('username') values ('ztz3 ')");

Var_dump ($ result );

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.