CURD and query method of thinkphp

Source: Internet
Author: User
Read Data $ m = newModel (& 039; User & 039;); $ m = M (& 039; User & 039;); select $ m-& gt; select (); get all data, return find $ m-& gt; find ($ id) in array form; get single data getField (field name) get a specific field

Read Data

$ 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

$ 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)

$ 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)

$ 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

$ Arr = $ m-> where ("sex = 0 and username = 'give'")-> find ();

B. array

$ 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.

$ Data ['sex'] = 0;

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

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

II. expression query method

$ 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

$ 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

$ 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

$ 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.

$ 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.

$ 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.