Home  >  Article  >  Backend Development  >  Introduction to thinkphp's CURD and query methods_PHP tutorial

Introduction to thinkphp's CURD and query methods_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:18:12760browse

Read dataRead

Copy code The code is as follows:

$m=new Model('User' );

$m=M('User');

select

$m->select();//Get all data and return it in array form

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');


3. Create data with ThinkPHP 3 (Key points)

Add dataCreate

Copy code The code is as follows:

$m=new Model('User') ;

$m=M('User');

$m->field name=value

$m->add();


The return value is the new id number

4. Delete data in ThinkPHP 3 (Key points)

Copy code The code is as follows:

$m=M(' User');

$m->delete(2); //Delete the data with id 2

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


The return value is Number of rows affected

5. ThinkPHP 3 update data (Key points)

Copy code The code is as follows:

$m=M(' User');

$data['id']=1;

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

$m->save($data);   


The return value is the number of affected rows

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

1. Common query methods

2. Expression query method

3. Interval query

4. Statistical query

5. SQL direct query

1. Common query methods

a. String

Copy code The code is as follows:

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

b, array
Copy code The code is as follows:

$data['sex']=0;

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

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


Note: This method defaults to the relationship of and. If you use the or relationship, you need to add Array value
Copy code The code is as follows:

$data['sex']=0;

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

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


2. Expression query method
Copy code The code is as follows:

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

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


EQ equals

NEQ is not equal to

GT is greater than

EGT is greater than or equal to

LT is less than

ELT less than or equal to

LIKE fuzzy query

Copy code The 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 middle of notlike

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


Note: If a field needs to match multiple wildcards

Copy the code The code is as follows:

$data['username']=array('like',array('%ge%','%2%','%五%'),'and');//if not The third value, the default relationship is the 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 ( `id` 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) )

3. Interval query

Copy code The code is 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',10),'or') //The relationship is the relationship of or

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


4. Statistical query

count //Get the number

max //Get the maximum number

min //Get the minimum number

avg //Get the average

sum //Get the sum

5. SQL direct query

a, query is mainly used for data processing

Result set of data returned successfully

Failure returns boolean false

Copy code The 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 a write operation

Successfully returns the number of affected rows

Failure returns boolean false

Copy code The code is as follows:

$m=M();

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

var_dump($result);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/621679.htmlTechArticleRead the data. The copy code is as follows: $m=new Model('User'); $m =M('User'); select $m-select();//Get all data and return it as an array find $m-find($id);//Get a single...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn