Add data insert
?
1 2 3 4 5 |
$data = [ 'name_cn' => '张三' , 'name_en' => 'jack' , ]; $res = Db::name( 'style' )->insert( $data ); |
adding data.
?
1 |
INSERT INTO `tf_style` (`name_cn` , `name_en`) VALUES ( '张三' , 'jack' ) |
tp5 can also insertAll
insert multiple data.
?
1 2 3 4 5 |
$data = [ 'name_cn' => '张三' , 'name_en' => 'jack' , ]; $res = Db::name( 'style' )->insertGetId( $data ); |
Get the inserted id.
Modify data update
Update data, how to use update
.
?
1 2 |
$res = Db::name( 'style' )->where( 'id' ,4)->update([ 'name_cn' => '李四' ]); UPDATE `tf_style` SET `name_cn` = '李四' WHERE `id` = 4; |
The returned result is the number of affected rows.
?
1 2 3 |
$where = new Where(); $where [ 'id' ] = [ '>' ,2]; $res = Db::name( 'style' )->where( $where )->update([ 'name_cn' => '李四' ]); |
Conditional operations through $ where object.
?
1 2 |
$where [] = [ 'id' , '>' ,2]; $res = Db::name( 'style' )->where( $where )->update([ 'name_cn' => '王五' ]); |
it is also fine.
The primary key can be directly written into the data data.
?
1 |
$res = Db::name( 'style' )->update([ 'name_cn' => '王五' , 'id' =>2]); |
The results are as follows:
?
1 |
UPDATE `tf_style` SET `name_cn` = '王五' WHERE `id` = 2; |
Only one piece of data can be modified in this way.
Only modify one field, use setField
method.
?
1 2 3 |
$res = Db::name( 'style' )->where( 'id' ,2)->setField([ 'name_cn' => '刘备' ]); $res = Db::name( 'style' )->where( 'id' ,2)->setField([ 'name_cn' => '刘备' , 'name_en' => 'LiuBei' ]); UPDATE `tf_style` SET `name_cn` = '刘备' , `name_en` = 'LiuBei' WHERE `id` = 2 |
The effect is update
almost the same.
Delete data
Delete one.
?
1 2 |
$res = Db::name( 'style' )->where( 'id' ,2)-> delete (); $res = Db::name( 'style' )-> delete ( '2' ); |
Delete multiple entries.
?
1 |
$res = Db::name( 'style' )-> delete ( '2,3' ); |
id is written in the string.
?
1 |
$res = Db::name( 'style' )-> delete ([2,3,4]); |
Or by id array.
Query dataselect
?
1 2 3 |
$data = Db::query( 'select * from tf_action' ); $data = Db::query( 'select * from tf_action where id > ? and id < ?' ,[1,10]); $sql = Db::getLastSql(); |
Use query for query.
Delete, add, modify, use execute.
?
1 |
$data = Db::table( 'tf_action' )->select(); |
The full name of the table is used here.
?
1 |
$data = Db::name( 'action' )->select(); |
The name of the table with the prefix removed is used here.
?
1 |
$data = db( 'action' )->select(); |
Helper function, the effect is Db::name
similar.
But they are not exactly the same.
?
1 |
$data = db( 'action' )->where( 'id' , '>' ,1)->where( 'id' , '<' ,9)->select(); |
Multi-condition query.
?
1 |
$data = db( 'action' )->where( 'id' , '>' ,20)->whereOr( 'id' , '<' ,9)->select(); |
Or query.
If the intermediate condition is empty, it means =.
?
1 2 3 4 5 6 7 |
$where = new Where(); $where [ 'name' ] = [ 'like' , '%户%' ]; $where [ 'id' ] = [ '>' ,1]; $data = db( 'action' )->where( $where )->select(); $where [] = [ 'name' , 'like' , '%户%' ]; $where [] = [ 'id' , '>' ,1]; $data = db( 'action' )->where( $where )->select(); |
Combination query.
?
1 2 3 4 |
$where = new Where(); $where [ 'name' ] = [ 'like' , '%户%' ]; $where [ 'id' ] = [ '>' ,1]; $data = db( 'action' )->where( $where )->limit(2,2)->order( 'id desc' )->select(); |
Sort by pages.
?
1 2 3 4 |
$where = new Where(); $where [ 'name' ] = [ 'like' , '%户%' ]; $where [ 'id' ] = [ '>' ,1]; $data = db( 'action' )->where( $where )->limit(2,2)->order( 'id desc' )->field( 'id,name' )->select(); |
Query the specified field.
?
1 2 3 4 |
$where = new Where(); $where [ 'name' ] = [ 'like' , '%户%' ]; $where [ 'id' ] = [ '>' ,1]; $data = db( 'action' )->where( $where )->limit(2,2)->order( 'id desc' )->field( 'id aid,name' )->select(); |
Alias.
?
1 |
$data = db( 'action' )->where( $where )->field( 'count(*) as count' )->find(); |
Use system functions.
?
1 |
$data = db( 'action' )->where( "name like '%户%' AND id > 1" )->select(); |
It is OK to write a string directly.