Introduction to thinkphp CURD and query methods

  • 2020-11-30 08:12:16
  • OfStack

Read for reading data


$m=new Model('User');
$m=M('User');
select
$m->select();// Gets all the data and returns it as an array 
find
$m->find($id);// Get a single piece of data 
getField( The field name )// To obtain 1 The value of a specific field 
$arr=$m->where('id=2')->getField('username');

3. ThinkPHP 3 Creating data (emphasis)

Add Create to data


$m=new Model('User');
$m=M('User');
$m-> The field name = value 
$m->add();

The return value is the new id number

4. ThinkPHP 3 Delete data (key)


$m=M('User');
$m->delete(2);               // delete id for 2 The data of 
$m->where('id=2')->delete(); // Same as above, also delete id for 2 The data of 

The return value is the number of rows affected

5. Update data of ThinkPHP 3 (key)


$m=M('User');
$data['id']=1;
$data['username']='ztz2';
$m->save($data);             

The return value is the number of rows affected

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

1. Ordinary query mode

2. Expression query mode

3. Interval query

4. Statistical query

5. SQL direct query

1. Ordinary query mode

a, strings


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

b, arrays,

$data['sex']=0;
$data['username']='gege';
$arr=$m->where($data)->find();

Note: This default is an and relationship, and 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 mode

$data['id']=array('lt',6);
$arr=$m->where($data)->select();

EQ equals

NEQ is not equal to

GT is greater than the

EGT is greater than or equal to

LT 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%'); //notlike There is no space in between 
    $arr=$m->where($data)->select();

Note: If one field matches more than one wildcard


$data['username']=array('like',array('%ge%','%2%','%5%'),'and');// If there is no regulation 3 The default relationship is or Relationship between 
$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));// Pay attention to, not  and  between In the middle 1 Make sure there are Spaces 
$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  and  The relationship between 
//SELECT * FROM `tp_user` WHERE ( (`id` > 4) AND (`id` < 10) ) 
$data['id']=array(array('gt',4),array('lt',10),'or') // Relationship is or The relationship between 
$data['name']=array(array('like','%2%'),array('like','%5%'),'gege','or');

4. Statistical query

count // gets the number

max // Gets the maximum number

min // Gets the minimum number

avg // Obtain averages

sum // gets the sum

5. SQL direct query

a and query mainly deal with reading data

The result set of the data returned successfully

Return boolean false on failure


$m=M();
$result=$m->query("select *  from t_user where id >50");
var_dump($result);

b, execute are used to update the write operation

Success returns the number of affected rows

Return boolean false on failure


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


Related articles: