thinkphp curd and Query method introduction _php Skills

Source: Internet
Author: User

Reading of data read

Copy Code code as follows:

$m =new Model (' User ');

$m =m (' User ');

Select

$m->select ();//Get all data, return as an array

Find

$m->find ($id);//Get a single piece of data

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

$arr = $m->where (' id=2 ')->getfield (' username ');


Iii. thinkphp 3 Creating data (emphasis)

Add a Create to data

Copy Code code as follows:

$m =new Model (' User ');

$m =m (' User ');

$m-> Field name = value

$m->add ();


The return value is the new ID number

Iv. thinkphp 3 Delete data (emphasis)

Copy Code code as follows:

$m =m (' User ');

$m->delete (2); Delete Data with ID 2

$m->where (' id=2 ')->delete (); Same effect as above, delete data with ID 2


The return value is the number of rows affected

V. thinkphp 3 Update data (FOCUS)

Copy Code code as follows:

$m =m (' User ');

$data [' id ']=1;

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

$m->save ($data);


The return value is the number of rows affected

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

First, the general inquiry method

Second, the expression of query method

Third, interval query

Iv. Statistical Inquiries

Five, SQL direct query

First, the general inquiry method

A, string

Copy Code code as follows:

$arr = $m->where ("Sex=0 and Username= ' Gege '")->find ();

B, Array
Copy Code code as follows:

$data [' Sex ']=0;

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

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


Note: This is by default the relationship between and, and if you use an or relationship, you need to add an array value
Copy Code code as follows:

$data [' Sex ']=0;

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

$data [' _logic ']= ' or ';


Second, the expression of query method
Copy Code code as follows:

$data [' id ']=array (' lt ', 6);

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


EQ equals

NEQ is not equal to

GT Greater than

EGT is greater than or equal to

LT is less than

ELT is less than or equal to

Like fuzzy query

Copy Code code as follows:

$data [' username ']=array (' like ', '%ge ');

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

Notlike

$data [' username ']=array (' notlike ', '%ge% '); No spaces in the middle of the notlike

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


Note: If a field is to match multiple wildcard characters

Copy Code code as follows:

$data [' username ']=array (' like ', Array ('%ge% ', '%2% ', '% Five '), ' and ');/If there is no third value, the default relationship is an or relationship

$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 there must be a space between not and between

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

In

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

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

SELECT * from ' tp_user ' WHERE (' IDs ' in (4,6,7))

$data [' id ']=array (' Not in ', Array (4,6,7));

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

SELECT * from ' tp_user ' WHERE (' id ' not in (4,6,7))

Third, interval query

Copy Code code as follows:

$data [' ID ']=array (Array (' GT ', 4), array (' LT ', 10)];/The default relationship is the relationship of and

SELECT * from ' Tp_user ' WHERE ((' ID ' > 4) and (' ID ' < 10))

$data [' ID ']=array (Array (' GT ', 4), array (' LT ', ten), ' or ')//relationship is the relationship between or

$data [' Name ']=array (' like ', '%2% '), array (' like ', '% Five '), ' gege ', ' or ');


Iv. Statistical Inquiries

Count//Get number

Max//Get maximum number

Min//Get minimum number

AVG//Get average

Sum//Get sum

Five, SQL direct query

A, query main number processing read data

Result set for successfully returning data

Failure returns Boolean false

Copy Code code as follows:

$m =m ();

$result = $m->query ("select * from T_user where ID >50");

Var_dump ($result);


B, execute is used to update a write operation

Number of rows affected successfully returned

Failure returns Boolean false

Copy Code code 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.