配置了数据库连接信息后 就可以直接使用数据库运行原生SQL操作了 支持query
(查询操作)和execute
(写入操作)方法 并且支持参数绑定。
Db::query('select * from think_user where id=?',[8]);
Db::execute('insert into think_user (id, name) values (?, ?)',[8,'thinkphp']);
也支持命名占位符绑定 例如:
Db::query('select * from think_user where id=:id',['id'=>8]);
Db::execute('insert into think_user (id, name) values (:id, :name)',['id'=>8,'name'=>'thinkphp']);
可以使用多个数据库连接 使用
Db::connect($config)->query('select * from think_user where id=:id',['id'=>8]);
$config是个单独的数据库配置 支持数组和字符串 也可以是个数据库连接的配置参数名。
尊贵的董事大人
英文标题不为空时 视为本栏投稿
需要关键字 描述 英文标题