tp5中多数据库操作

来源:互联网 发布:python缺省参数 编辑:程序博客网 时间:2024/06/06 03:59
在application/config.php中加入配置
 '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);