1. 数据库配置
2. query execute原生态sql语句 增删改查$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);
3. 参数绑定 命名占位符绑定$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']);
4. 查询构造器
//添加: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]);
5. db链式操作支持链式查询的方法:
方法名
描述
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的数据库操作,更多相关内容请关注。
相关推荐:
thinkphp 分布式数据库详解
如何通过thinkphp链接数据库
如何通过thinkphp连接多数据库
以上就是关于thinkphp5的数据库操作的详细内容。