|
1、数据库配置
app/database.php中配置账号密码等信息
2、query execute 原生态 sql 语句,增删改查 使用时请去掉@字符
- //插入记录
- $result = Db::exe@cute('ins@ert into tp_data (id,name,status) values (2,"222","abc") ');
- $du@mp($result);
- //更新记录
- $result = Db::exe@cute('up@date tp_data set name="5ucms.com" where id = 5 ');
- $du@mp($result);
- //查询数据
- $result = Db::exe@cute('sel@ect * from tp_data where id=5 ');
- $du@mp($result);
- //删除数据
- $result = Db::exe@cute('del@ete from tp_data where id=5 ');
- $du@mp($result);
- //显示数据库表结构
- $result = Db::qu@ery('show tables from tp_data')
- print_r($result)
- //清空数据列表
- $result = Db::qu@ery('TRU@NCATE table tp_data')
- du@mp($result)
复制代码
3、参数绑定,命名占位符绑定
- //参数绑定,先预处理,再导入数据
- Db::exe@cute('in@sert into tp_data (id,name,status) values (?,?,?)',[3,'tp5',1])
- $result = Db::qu@ery('se@lect * from tp_data where id = ?', [3]);
- print_r($result);
- //命名占位符绑定 [=>里的方法,可以和上边一样写简单一维数组]
- Db::exe@cute('in@sert into tp_data (id,name,status) values (:id,:name,:status)',['id' = > 3,'name' = > 'tp5','status' = > 1])
- $result = Db::qu@ery('se@lect * from tp_data where id = :id', ['id' = > 3]);
- print_r($result);
复制代码
4、多个数据库切换查询操作
需要在app/config.php中配置 db2 的数据库,数组格式参考database.php中内容
有第三个可以写db3
- $result = Db::connect('db2')->qu@ery('sel@ect * from tp_data where id=5 ');
- print_r($result)
- $result = Db::connect('db3')->qu@ery('sel@ect * from tp_data where id=21');
- print_r($result)
- //或
- $db1 = Db::connect('db2');
- $db1->qu@ery('sel@ect * from tp_data where id=5 ');
复制代码
|
|