增删改查是数据库的基本操作, 下面总结一下thinkphp5.0关于数据库的增删改查的方法.
- 增添数据
(1)使用Db类的 insert 方法向数据库中提交数据
d
a
t
a
=
[
′
n
a
m
e
′
=
>
′
z
h
a
n
g
s
a
n
′
,
′
a
g
e
′
=
>
′
3
0
′
]
;
D
b
:
:
t
a
b
l
e
(
′
u
s
e
r
′
)
−
>
i
n
s
e
r
t
(
data = ['name'=>'zhangsan', 'age'=>'30']; Db::table('user')->insert(
data=[′name′=>′zhangsan′,′age′=>′30′];Db::table(′user′)−>insert(data); //向 user 表中添加一条记录
1
2
如果你在 database.php 配置文件中配置了数据库前缀(prefix), 那么可以直接使用 Db 类的 name 方法提交数据,例如:
Db::name(‘user’)->insert($data); //向 user 表中添加一条记录
1
注: insert 方法添加数据成功返回添加成功的条数, insert 正常情况返回1.
添加数据的同时返回主键值的方法:
a.使用 getLastInsID 方法返回新增数据的自增主键值:
Db::name(‘user’)->insert($data);
$userId = Db::name(‘user’)->getLastInsID(); //返回新增数据主键的值
1
2
b.使用 insertGetId 方法新增数据并返回主键值:
Db::name(‘user’)->insertGetId($data); //在向数据库添加记录的同时返回主键值
1
注: insertGetId 方法只在添加数据成功时才返回添加数据自增主键的值
(2)添加多条数据
添加多条数据直接向 Db 的insertsAll 方法传入需要添加的数据即可, 例如:
d
a
t
a
=
[
[
′
n
a
m
e
′
=
>
′
z
h
a
n
g
s
a
n
′
,
′
a
g
e
′
=
>
′
2
0
′
]
,
[
′
n
a
m
e
′
=
>
′
l
i
s
i
′
,
′
a
g
e
′
=
>
′
2
1
′
]
,
[
′
n
a
m
e
′
=
>
′
w
a
n
g
w
u
′
,
′
a
g
e
′
=
>
′
2
2
′
]
]
;
D
b
:
:
n
a
m
e
(
′
u
s
e
r
′
)
−
>
i
n
s
e
r
t
A
l
l
(
data = [ ['name'=>'zhangsan', 'age'=>'20'], ['name'=>'lisi', 'age'=>'21'], ['name'=>'wangwu', 'age'=>'22'] ]; Db::name('user')->insertAll(
data=[[′name′=>′zhangsan′,′age′=>′20′],[′name′=>′lisi′,′age′=>′21′],[′name′=>′wangwu′,′age′=>′22′]];Db::name(′user′)−>insertAll(data);
1
2
3
4
5
6
注: insertAll 方法添加数据成功返回添加成功的条数
(3)助手函数
//添加单条数据
db(‘user’)->insert(
d
a
t
a
)
;
/
/
添
加
多
条
数
据
d
b
(
′
u
s
e
r
′
)
−
>
i
n
s
e
r
t
(
data); //添加多条数据 db('user')->insert(
data);//添加多条数据db(′user′)−>insert(list);
1
2
3
4
注意: 使用助手函数默认每次都会重新连接数据库, 而使用Db::naem 或者 Db::table 方法的话都是单例的(单例模式:作为对象的创建模式,单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统全局地提供这个实例。它不会创建实例副本,而是会向单例类内部存储的实例返回一个引用. 单例模式在操作包含大量数据的数据库时可以避免多次 new 操作 ).
2.删除数据
(1)使用 Db 类的 delete 方法删除数据
//根据主键删除
Db::table(‘users’)->delete(1); // 删除主键为1的记录
Db::table(‘user’)->delete([1, 2, 3]); //删除主键为1,2,3的记录
//根据条件删除
Db::table(‘user’)->where(‘id’, 1)->delete(); //删除主键值为1的记录
Db::table(‘user’)->where(‘id’, ‘<’, 10)->delete(); //删除主键值小于10的记录
1
2
3
4
5
6
(2)助手函数
//根据主键删除
db(‘user’)->delete(1);
//条件删除
db(‘user’)->where(‘id’, 1)->delete();
1
2
3
4
3.更新数据(修改数据)
(1)更新数据表中的值
Db::table(‘user’)->where(‘id’, 1)->update([‘name’=>‘zhangsan’, ‘age’=>‘30’]);
1
如果数据中含有主键, 则可以直接使用
Db::table(‘user’)->update(‘name’=>‘zhangsan’, ‘age’=>‘30’, ‘id’=>‘5’);
1
注:
a. 这种方式 update 方法中必须有传入主键的值, 在此例中 id 为主键, 若 update 方法中没有 传入 id 参数, 则无法完成信息的修改.
b. update 方法返回的是影响数据的条数, 没有修改任何数据则返回 0 .
如果要更新的数据需要使用SQL函数或者其他字段, 可以使用下面的方式:
Db::table(‘user’)
->where(‘id’, 1)
->update([
‘login_time’=>[‘exp’, ‘now()’], //调用 now 函数获取当前时间
‘login_times’=>[‘exp’, login_times + 1] //使用login_times字段
]);
1
2
3
4
5
6
(2)更新某个字段的值
Db::table(‘user’)->where(‘id’, 1)->setField(‘name’, ‘zhangsan’);
1
注: setField 方法返回的是影响的条数, 没有修改任何数据时返回 0 .
(3)自增或者自减一个字段的值
Db::table(‘user’)->where(‘id’, 1)->setInc(‘score’); // score 自增1
Db::table(‘user’)->where(‘id’, 1)->setInc(‘score’, 3) //score自增3
Db::table(‘user’)->where(‘id’, 1)->setDec(‘score’); //score自减1
Db::table(‘user’)->where(‘id’, 1)->setDec(‘score’, 5); //score 自减5
1
2
3
4
注: setInc / setDec 方法返回值是影响数据的条数, 如不加第二个参数, 则默认值为 1.
(3)延迟更新
setInc / setDec 函数支持延时更新, 如果需要延迟更新则需要传入第三个参数.
Db::table(‘user’)->where(‘id’, 1)->setInc(‘score’, 1, 10); //score延迟10秒自增1
1
(4)助手函数
//更新数据表中的数据
db(‘user’)->where(‘id’, 1)->update([‘name’=>‘zhangsan’]);
//更新某个字段的值
db(‘user’)->where(‘id’, 1)->setField(‘name’, ‘zhangsan’);
//自增 score 字段
db(‘user’)->where(‘id’, 1)->seInc(‘score’);
//自减 score 字段
db(‘user’)->where(‘id’, 1)->setDec(‘score’);
1
2
3
4
5
6
7
8
(5)快捷更新
V5.0.5+以上封装的 data, inc, dec 和 exp 方法属于链式操作方法, 可以配合update 使用, 例如:
Db::table(‘user’)
->where(‘id’, 2)
->inc(‘read’) //read 字段值自增1
->dec(‘score’, 3) // score 字段值自减3
->exp(‘name’, ‘UPPER(name)’) //将name字段值变成大写
->update();
1
2
3
4
5
6
4. 查找数据
(1) 基本查询
查询一个数据:
//table 方法必须指定完整的数据表名
Db::table(‘user’)->where(‘id’, 1)->find();
1
2
注: find 方法若查询不到结果,则返回 null .
查询数据集使用:
Db::table(‘user’)->where(‘id’, 1)->select();
1
注: select 方法查询不到结果, 则返回空数组.
如果设置了数据表前缀参数的话, 可以使用:
Db::name(‘user’)->where(‘id’, 1)->find();
Db::name(‘user’)->where(‘id’, 1)->select();
1
2
注: 如果你的数据表中没有使用前缀功能, 那么name 和 find 方法的效果是一样的; find 和 select 方法返回的都是数组.
(2)助手函数
系统提供了一个 db 助手函数, 可以更方便的查询:
db(‘user’)->where(‘id’, 1)->find();
db(‘user’)->where(‘id’, 1)->select();
1
2
(3)使用Query对象或者闭包查询
a. 使用query 查询对象进行查询, 例如:
$query = new \think\db\Query();
q
u
e
r
y
−
>
t
a
b
l
e
(
′
u
s
e
r
′
)
−
>
w
h
e
r
e
(
′
i
d
′
,
1
)
;
D
b
:
:
f
i
n
d
(
query->table('user')->where('id', 1); Db::find(
query−>table(′user′)−>where(′id′,1);Db::find(query);
Db::select($query);
1
2
3
4
b. 使用闭包函数进行查询, 例如:
Db::select(function($query)) {
$query->table(‘user’)->where(‘id’, 1);
}
1
2
3
(4)值和列的查询
a.查询某个字段的值:
Db::table(‘user’)->where(‘id’, 1)->value(‘name’);
1
注: 当value 方法查询结果不存在时,返回 null .
b.查询某一列的值:
//返回数组
Db::table(‘user’)->where(‘id’, 1)->column(‘name’);
//指定索引
Db::table(‘user’)->where(‘id’, 1)->column(‘name’, ‘id’); //返回的数组的索引值为id
1
2
3
4
注: column 方法查询结果不存在时,返回的是空数组.
以上就是thinkphp5.0 对数据库的增删改查操作, 希望对你有所帮助. 下面是测试代码:
<?php namespace app\index\controller; use think\Db; class Index { public function index() { } //连接数据库并查询数据 public function data() { //实例化Db类 //$DB = new Db; //查询数据 //$data = $DB::table("user")->select(); //$data = $DB::query("select * from user"); //$data = Db::name('user')->where('user_id', 1)->find(); //$data = Db::table('user')->where('user_id', 1)->select(); //使用助手函数查询数据,助手函数默认会每次都会连接数据库 //$data = db('user')->where("user_id", 1)->find(); //$data = db('user', [], false)->where('user_id', 1)->select(); //使用Query对象或闭包查询 //使用查询对象进行查询 //$query = new \think\db\Query(); // $query->table('user')->where('user_id', 1); /*$data = Db::find($query);*/ //使用闭包函数查询 // $data = Db::select(function($query) { // $query->table('user')->where('user_id', 1); // }); //查询某个字段的值 // $data = Db::table('user')->where('user_id', 1)->value('user_tel'); // 查询某一列的值 // $data = Db::table('user')->where('user_id', 1)->column('user_tel'); //返回数组 // dump($data); $data = Db::table('user')->where('user_id', 1)->column('user_tel', 'password'); //指定索引 // $data = Db::table('user')->where('user_id', 1)->column('user_id, user_tel'); //也能返回索引 //数据集分批处理 // Db::table('user')->chunk(5, function($users) { // foreach ($users as $user) { // // print_r($user); // } // }, 'hometown'); //JSON类型数据查询 // Db::table('user')->where('hometown', '南方')->find(); dump($data); } //使用方法配置连接数据库 //使用数组 /* public function data1() { echo "使用方法配置连接数据库"; $DB = Db::connect([ // 数据库类型 'type' => 'mysql', // 服务器地址 'hostname' => '127.0.0.1', // 数据库名 'database' => 'eating', // 用户名 'username' => 'root', // 密码 'password' => '5386', // 端口 'hostport' => '3306' ]); $data = $DB->table("user")->select(); dump($data); //使用字符串 $Db = Db::connect("mysql://root:5386@127.0.0.1:3306/eating#utf8"); $data = $Db->table("user")->select(); dump($data); } //使用模型连接数据库 public function data2() { echo "使用模型连接数据库"; $user = new \app\index\model\User(); dump($user::all()); }*/ public function insert() { $data = ['user_tel'=>'15955702565', 'password'=>'654321', 'hometown'=>'北方', 'tasty'=>'轻口味', 'type_of_cooking'=>'干']; // Db::table('user')->insert($data); // $res = Db::name('user')->insert($data); // dump(Db::name('user')->insertGetId($data)); // db('user')->insert($data); // Db::table('user') // ->data(['user_tel'=>'15955702565', 'password'=>'654321', 'hometown'=>'北方', 'tasty'=>'轻口味', 'type_of_cooking'=>'干']) // ->insert(); // dump($res); } public function update() { // $res = Db::table('user')->where('user_id', 3)->update(['user_tel'=>'98765432100']); // $res = Db::table('user')->update(['user_tel'=>'98765432101','password'=>'123456789', 'user_id'=>'5']); // $res = Db::table('user')->where('user_id', 1)->setInc('user_tel', 3); // dump($res); db('user')->where('user_id', 5)->update(['user_tel'=>'98765432102']); } }