Summary of common operations of adding deleting modifying and checking Db in tp5 of thinkPHP5 framework database

  • 2021-11-13 01:10:05
  • OfStack

This paper describes the common operation of tp5 (thinkPHP5) framework database Db. Share it for your reference, as follows:

Add data insert


$data = [
  'name_cn' => ' Zhang 3',
  'name_en' => 'jack',
];
$res = Db::name('style')->insert($data);

Add data.


INSERT INTO `tf_style` (`name_cn` , `name_en`) VALUES (' Zhang 3' , 'jack')

tp5 is also available through insertAll Insert multiple pieces of data.


$data = [
  'name_cn' => ' Zhang 3',
  'name_en' => 'jack',
];
$res = Db::name('style')->insertGetId($data);

Gets the inserted id.

Modify data update

Update data, using update Method.


$res = Db::name('style')->where('id',4)->update(['name_cn'=>' Li 4']);
UPDATE `tf_style` SET `name_cn` = ' Li 4' WHERE `id` = 4 ; 

Returns the number of rows affected as a result.


$where = new Where();
$where['id'] = ['>',2];
$res = Db::name('style')->where($where)->update(['name_cn'=>' Li 4']);

Conditional operations are performed through the $where object.


$where[] = ['id','>',2];
$res = Db::name('style')->where($where)->update(['name_cn'=>' Wang 5']);

It's ok, too.

The primary key can be written directly to data data.


$res = Db::name('style')->update(['name_cn'=>' Wang 5','id'=>2]);

The results are as follows:


UPDATE `tf_style` SET `name_cn` = ' Wang 5' WHERE `id` = 2;

Only one piece of data can be modified in this way.

Modify only 1 field, using the setField Method.


$res = Db::name('style')->where('id',2)->setField(['name_cn'=>' Liu Bei ']);
$res = Db::name('style')->where('id',2)->setField(['name_cn'=>' Liu Bei ','name_en'=>'LiuBei']);
UPDATE `tf_style` SET `name_cn` = ' Liu Bei ' , `name_en` = 'LiuBei' WHERE `id` = 2

Effect and update Almost.

Delete data delete

Delete 1 article.


$res = Db::name('style')->where('id',2)->delete();
$res = Db::name('style')->delete('2');

Delete multiple items.


INSERT INTO `tf_style` (`name_cn` , `name_en`) VALUES (' Zhang 3' , 'jack')

0

id is written in a string.


INSERT INTO `tf_style` (`name_cn` , `name_en`) VALUES (' Zhang 3' , 'jack')

1

Or through the id array.

Query data select


INSERT INTO `tf_style` (`name_cn` , `name_en`) VALUES (' Zhang 3' , 'jack')

2

Query with query.

Delete, add, modify, use execute.


$data = Db::table('tf_action')->select();

The full name of the table is used here.


INSERT INTO `tf_style` (`name_cn` , `name_en`) VALUES (' Zhang 3' , 'jack')

4

The table name with the prefix removed is used here.


INSERT INTO `tf_style` (`name_cn` , `name_en`) VALUES (' Zhang 3' , 'jack')

5

Helper function, effect and Db::name Almost.

But it's not exactly the same.


INSERT INTO `tf_style` (`name_cn` , `name_en`) VALUES (' Zhang 3' , 'jack')

6

Multi-condition query.


INSERT INTO `tf_style` (`name_cn` , `name_en`) VALUES (' Zhang 3' , 'jack')

7

Or query.

If the middle condition is empty, it means =.


$where = new Where();
$where['name'] = ['like','% Household %'];
$where['id'] = ['>',1];
$data = db('action')->where($where)->select();
$where[] = ['name','like','% Household %'];
$where[] = ['id','>',1];
$data = db('action')->where($where)->select();

Composite queries.


INSERT INTO `tf_style` (`name_cn` , `name_en`) VALUES (' Zhang 3' , 'jack')

9

Page sorting.


$where = new Where();
$where['name'] = ['like','% Household %'];
$where['id'] = ['>',1];
$data = db('action')->where($where)->limit(2,2)->order('id desc')->field('id,name')->select();

Query the specified field.


$where = new Where();
$where['name'] = ['like','% Household %'];
$where['id'] = ['>',1];
$data = db('action')->where($where)->limit(2,2)->order('id desc')->field('id aid,name')->select();

Give me an alias.


$data = db('action')->where($where)->field('count(*) as count')->find();

Use system functions.


$data = [
  'name_cn' => ' Zhang 3',
  'name_en' => 'jack',
];
$res = Db::name('style')->insertGetId($data);

3

Writing strings directly is also OK's.

For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: