tp5中多数据库操作
在application/config.php中加入配置
"db2" => [
"type" => "mysql", // 数据库类型
"hostname" => "127.0.0.1", / /服务器地址
"database" => "tpshop2", // 数据库名
"username" => "root", // 数据库用户名
"password" => "", // 数据库密码
"hostport" => "", // 数据库连接端口
"params" => [], // 数据库连接参数
"charset" => "utf8", // 数据库编码默认采用utf8
"prefix" => "tp_", // 数据库表前缀
"db2" => [
"type" => "mysql", // 数据库类型
"hostname" => "127.0.0.1", / /服务器地址
"database" => "tpshop2", // 数据库名
"username" => "root", // 数据库用户名
"password" => "", // 数据库密码
"hostport" => "", // 数据库连接端口
"params" => [], // 数据库连接参数
"charset" => "utf8", // 数据库编码默认采用utf8
"prefix" => "tp_", // 数据库表前缀
],
依次类推
//connect为链接数据库
//获取数据库对象,然后再操作
$result = Db::connect("db2")->query("select * from sb_ad where ad_id = 1");
print_r($result);
$result = Db::connect("db3")->query("select * from sb_ad where ad_id = 1");
print_r($result);
//参数绑定
Db::execute("insert into sb_ad (ad_name, ad_content ,status) values (?, ?, ?)", [3, "thinkphp", 1]);
$result = Db::query("select * from sb_ad where ad_id = ?", [3]);
print_r($result);
/******命名占位符绑定*****/
Db::execute("insert into sb_ad (ad_name, ad_content ,status) values (:ad_name, :ad_content, :status)", ["ad_name" => 11, "ad_content" => "thinkphp", "status" => 1]);
$result = Db::query("select * from sb_ad where ad_id=:id", ["id" => 10]);
print_r($result);
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。