Home > Article > Backend Development > thinkphp's CURD and query methods_PHP tutorial
Reading of data Read
$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 in ThinkPHP 3 (Key points)
Adding data Create
$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)
$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 the number of affected rows
5. ThinkPHP 3 update data (key points)
$m=M('User');
$data['id']=1;
$data['username']='ztz2';
$m->save($data);
The return value is the number of affected rows
============================================
1. Ordinary query methods
2. Expression query method
3. Interval query
4. Statistical query
5. SQL direct query
1. Ordinary query methods
a. String
$arr=$m->where("sex=0 and username='gege'")->find();
b. Array
$data['sex']=0;
$data['username']='gege';
$arr=$m->where($data)->find();
Note: This method defaults to an and relationship. If you use an or relationship, you need to add an array value
$data['sex']=0;
$data['username']='gege';
$data['_logic']='or';
2. Expression query method
$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 is less than or equal to
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 middle of notlike
$arr=$m->where($data)->select();
Note: If a field needs to match multiple wildcard characters
$data['username']=array('like',array('%ge%','%2%','%五%'),'and');//If there is no third value, the default The relationship is 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, 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
$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`
$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 successfully returned data
Returns boolean false
on failure$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
Returns boolean false
on failure$m=M();
$result=$m->execute("insert into t_user(`username`) values('ztz3')");
var_dump($result);