Home >Backend Development >PHP Tutorial >Introduction to thinkphp's CURD and query methods_PHP tutorial
Read dataRead
$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');
Add dataCreate
$m=M('User');
$m->field name=value
$m->add();
4. Delete data in ThinkPHP 3 (Key points)
$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
5. ThinkPHP 3 update data (Key points)
$data['id']=1;
$data['username']='ztz2';
$m->save($data);
==============================================
1. Common query methods
2. Expression query method
3. Interval query
4. Statistical query
5. SQL direct query
1. Common query methods
a. String
$data['username']='gege';
$arr=$m->where($data)->find();
$data['username']='gege';
$data['_logic']='or';
$arr=$m->where($data)->select();
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
$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
$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
//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');
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
$result=$m->query("select * from t_user where id >50");
var_dump($result);
Successfully returns the number of affected rows
Failure returns boolean false
$result=$m->execute("insert into t_user(`username`) values('ztz3')");
var_dump($result);