时间:2021-07-01 10:21:17 帮助过:35人阅读

$result = Db::execute('insert into log(user_id, ip) values(1, 11231)');
dump($result);
$result = Db::query('select * from log');
echo '<pre>';
var_dump($result);$str = 'insert into log(user_id, ip) values(?, ?)';
$result = Db::execute($str, [1, '12312']);
$result = Db::query('select * from log where id = ?', [4]);
//占位符
Db::execute('insert into log(user_id, ip) values(:user_id, :ip)', ['user_id'=>12, 'ip'=>'5555']);//添加:
Db::table('log')->insert(['user_id'=>1, 'ip'=>'654321']);
//更新
Db::table('log')
->where('id', 12)
->update(['user_id'=>123]);
//查询数据
$list = Db::table('log')
->where('id', 12)
->select();
//删除数据
Db::table('log')
->where('id', 10)
->delete();查询表时不用加前缀的方法:
Db::name('log')->insert(['user_id'=>44, 'ip'=>5555]);支持链式查询的方法:
方法名 | 描述 |
select | 查询数据库 |
find | 查询单个记录 |
insert | 插入记录 |
update | 更新记录 |
dalete | 删除记录 |
value | 查询值 |
column | 查询列 |
chunk | 分块查询 |
count | 聚合查询 |
6. 事物支持
//自动控制事物
Db::transaction(function (){
Db::table('log')->delete(2);
Db::table('log')->insert(['user_id'=>123]);
});
//手动控制事物的提交
//启动事物
Db::startTrans();
try {
Db::table('log')
->where(2);
Db::table('log')
->insert(['user_id' => 213]);
Db::commit();
} catch (Exception $e){
Db::rollback();
}本文讲解了关于thinkphp5的数据库操作,更多相关内容请关注Gxl网。
相关推荐:
thinkphp 分布式数据库详解
如何通过ThinkPHP链接数据库
如何通过thinkphp连接多数据库
以上就是关于thinkphp5的数据库操作的详细内容,更多请关注Gxl网其它相关文章!