1、数据库配置
database.php 进行配置
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'demo1',
// 用户名
'username' => 'root',
// 密码
'password' => 'vertrigo',
// 端口
'hostport' => '',
// 连接dsn
'dsn' => '',
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => 'utf8',
// 数据库表前缀
'prefix' => 'tp_',
2、query execute 原生态SQL语句 增删改查
//插入记录 返回结果为影响行数
$result = Db::execute('insert into tp_data (id,name,status) values (20,"222",1)');
//更新记录 返回结果为影响行数
$result = Db::execute('update tp_data set name = "framework1" where id = 5');
//查询数据
$result = Db::query('select * from tp_data where id= 5');
//删除数据
$result = Db::execute('delete from tp_data where id =5');
//显示数据库列表
$result = Db::query('show tables from demo1');
//清空数据表
$result = Db::execute('TRUNCATE table tp_data');
返回结果:
D:\Program Files\VertrigoServ\www\tp5\thinkphp\library\think\Debug.php:193:int 1
D:\Program Files\VertrigoServ\www\tp5\thinkphp\library\think\Debug.php:193:int 1
D:\Program Files\VertrigoServ\www\tp5\thinkphp\library\think\Debug.php:193:
array (size=1)
0 =>
array (size=3)
'id' => int 5
'name' => string 'framework1' (length=10)
'status' => int 1
D:\Program Files\VertrigoServ\www\tp5\thinkphp\library\think\Debug.php:193:int 1
D:\Program Files\VertrigoServ\www\tp5\thinkphp\library\think\Debug.php:193:
array (size=7)
0 =>
array (size=1)
'Tables_in_demo1' => string 'tp_area_region' (length=14)
1 =>
array (size=1)
'Tables_in_demo1' => string 'tp_car' (length=6)
2 =>
array (size=1)
'Tables_in_demo1' => string 'tp_comment' (length=10)
3 =>
array (size=1)
'Tables_in_demo1' => string 'tp_data' (length=7)
4 =>
array (size=1)
'Tables_in_demo1' => string 'tp_region' (length=9)
5 =>
array (size=1)
'Tables_in_demo1' => string 'tp_shipping_area' (length=16)
6 =>
array (size=1)
'Tables_in_demo1' => string 'tp_users' (length=8)
3、参数绑定 命名占位符绑定
//参数绑定
Db::execute('insert into tp_data (id,name,status) values (?,?,?)',[25,'thinkphp',1]);
$result = Db::query('select * from tp_data where id =?',[25]);
dump($result);
结果:
D:\Program Files\VertrigoServ\www\tp5\thinkphp\library\think\Debug.php:193:
array (size=1)
0 =>
array (size=3)
'id' => int 25
'name' => string 'thinkphp' (length=8)
'status' => int 1
//命名占位符绑定
Db::execute('insert into tp_data(id,name,status) values (:id,:name,:status)',['id'=>33,'name'=>'thinkphp','status'=>1]);
//也可以使用
//Db::execute('insert into tp_data(id,name,status) values (:id,:name,:status)',[33,'thinkphp',1]);也是一样的效果
$result = Db::query('select * from tp_data where id = :id',['id' =>10]);
dump($result);
结果:
D:\Program Files\VertrigoServ\www\tp5\thinkphp\library\think\Debug.php:193:
array (size=1)
0 =>
array (size=3)
'id' => int 10
'name' => string '王刚' (length=6)
'status' => int 0
4、多个数据库切换查询操作
第一步需要在config.php里分别配置db1、db2
'db1' =>[
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'demo1',
// 用户名
'username' => 'root',
// 密码
'password' => 'vertrigo',
// 端口
'hostport' => '',
// 连接dsn
'dsn' => '',
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => 'utf8',
// 数据库表前缀
'prefix' => 'tp_',
],
'db2' =>[
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'demo',
// 用户名
'username' => 'root',
// 密码
'password' => 'vertrigo',
// 端口
'hostport' => '',
// 连接dsn
'dsn' => '',
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => 'utf8',
// 数据库表前缀
'prefix' => 'tp_',
],
第二步在控制器里
//首先在config.php 配置两个DB1、DB2两个数据加配置
$result = Db::connect('db1')->query('select * from tp_data where id =6');
dump($result);
$result = Db::connect('db2')->query('select * from tp_data where id =7');
dump($result);
//也可以使用 先链接
$db1 = Db::connect('db1');
$db2 = Db::connect('db2');
$db1->query('select * from tp_data where id =1');
$db2->query('select * from tp_data where id =1');
第三步结果为
D:\Program Files\VertrigoServ\www\tp5\thinkphp\library\think\Debug.php:193:
array (size=1)
0 =>
array (size=3)
'id' => int 6
'name' => string 'thinkphp' (length=8)
'status' => int 1
D:\Program Files\VertrigoServ\www\tp5\thinkphp\library\think\Debug.php:193:
array (size=1)
0 =>
array (size=3)
'id' => int 7
'name' => string 'thinkphp' (length=8)
'status' => int 1