thinkphp 框架數(shù)據(jù)庫切換實現(xiàn)方法分析
本文實例講述了thinkphp 框架數(shù)據(jù)庫切換實現(xiàn)方法。分享給大家供大家參考,具體如下:
數(shù)據(jù)庫配置:
//數(shù)據(jù)庫配置1 'db_config1' => [ // 數(shù)據(jù)庫類型 'type' => 'mysql', // 服務器地址 'hostname' => '127.0.0.1', // 數(shù)據(jù)庫名 'database' => 'thinkphp', // 數(shù)據(jù)庫用戶名 'username' => 'root', // 數(shù)據(jù)庫密碼 'password' => '', // 數(shù)據(jù)庫編碼默認采用utf8 'charset' => 'utf8', // 數(shù)據(jù)庫表前綴 'prefix' => 'think_', ], //數(shù)據(jù)庫配置2 'db_config2' => 'mysql://root:1234@localhost:3306/thinkphp#utf8';
//默認數(shù)據(jù)庫讀取數(shù)據(jù) $test = Db::name("test")->select(); //第二個數(shù)據(jù)庫讀取數(shù)據(jù) $test1=Db::connect("DB_Config_1")->name("test")->select();
application/config.php
$db1 = [ 'type'=>'mysql', 'hostname'=>'127.0.0.1', 'database'=>'testA', 'username'=>'root', 'password'=>'123456', 'hostport'=>'3306', 'params'=>[], 'charset'=>'utf8', 'prefix'=>'', ], $db2 = [ 'type'=>'mysql', 'hostname'=>'127.0.0.1', atabase'=>'testB', 'username'=>'root', 'password'=>'123456', 'hostport'=>'3306', 'params'=>[], 'charset'=>'utf8', 'prefix'=>'', ], Db::connect('db1')->query('select * from user where age=25');
方法配置
我們可以在調(diào)用Db類的時候動態(tài)定義連接信息,例如:
Db::connect([ // 數(shù)據(jù)庫類型 'type' => 'mysql', // 數(shù)據(jù)庫連接DSN配置 'dsn' => '', // 服務器地址 'hostname' => '127.0.0.1', // 數(shù)據(jù)庫名 'database' => 'thinkphp', // 數(shù)據(jù)庫用戶名 'username' => 'root', // 數(shù)據(jù)庫密碼 'password' => '', // 數(shù)據(jù)庫連接端口 'hostport' => '', // 數(shù)據(jù)庫連接參數(shù) 'params' => [], // 數(shù)據(jù)庫編碼默認采用utf8 'charset' => 'utf8', // 數(shù)據(jù)庫表前綴 'prefix' => 'think_', ]);
或者使用字符串方式:
Db::connect('mysql://root:1234@127.0.0.1:3306/thinkphp#utf8');
字符串連接的定義格式為:
數(shù)據(jù)庫類型://用戶名:密碼@數(shù)據(jù)庫地址:數(shù)據(jù)庫端口/數(shù)據(jù)庫名#字符集
注意:字符串方式可能無法定義某些參數(shù),例如前綴和連接參數(shù)。
如果我們已經(jīng)在應用配置文件(注意這里不是數(shù)據(jù)庫配置文件)中配置了額外的數(shù)據(jù)庫連接信息,例如:
//數(shù)據(jù)庫配置1 'db_config1' => [ // 數(shù)據(jù)庫類型 'type' => 'mysql', // 服務器地址 'hostname' => '127.0.0.1', // 數(shù)據(jù)庫名 'database' => 'thinkphp', // 數(shù)據(jù)庫用戶名 'username' => 'root', // 數(shù)據(jù)庫密碼 'password' => '', // 數(shù)據(jù)庫編碼默認采用utf8 'charset' => 'utf8', // 數(shù)據(jù)庫表前綴 'prefix' => 'think_', ], //數(shù)據(jù)庫配置2 'db_config2' => 'mysql://root:1234@localhost:3306/thinkphp#utf8';
我們可以改成
Db::connect('db_config1'); Db::connect('db_config2');
database.php是框架默認的數(shù)據(jù)庫配置,里面寫數(shù)據(jù)庫1的信息,新建了個database2.php是放置數(shù)據(jù)庫2的信息。
創(chuàng)建完數(shù)據(jù)庫2之后,在config配置文件里,文件最后引入數(shù)據(jù)庫2的配置信息
$db_con2 = require_once ('database2.php'), 'db_con2' => $db_con2,
代碼中引用:
選擇數(shù)據(jù)庫1的時候,我是用模型查詢的直接寫SQL語句:
//模型查詢 $user = new User(); $result = $user->where('username', $data['username']) ->where('password', $data['password']) ->find();
或者
User::where('id','1')->find(); //普通結構查詢 Db::table('think_user')->where('id',1)->find();
查詢數(shù)據(jù)庫2的信息時,調(diào)用普通查詢語句:
$list = Db::connect('db_con2') ->table('nrf_amf_reg_info') ->alias('r') ->join('nrf_amf_server s','r.Id = s.nrf_amf_reg_Id','LEFT') ->paginate();
或者
$list = Db::connect('db_con2')->name('nrf_disc_record')->paginate();
注:nrf_amf_reg_info和nrf_disc_record為表名
更多關于thinkPHP相關內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結》、《ThinkPHP常用方法總結》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術總結》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設計有所幫助。
相關文章
PHP全局變量中的global與$GLOBALS的區(qū)別和用法小結
這篇文章主要介紹了PHP全局變量中的global與$GLOBALS的區(qū)別和用法小結,global的作用就相當于傳遞參數(shù),在函數(shù)外部聲明的變量,如果在函數(shù)內(nèi)想要使用,就用global來聲明該變量,這樣就相當于把該變量傳遞進來了,就可以引用該變量了,需要的朋友可以參考下2023-10-10Bootstrap+PHP實現(xiàn)多圖上傳功能實例詳解
這篇文章主要介紹了Bootstrap+PHP實現(xiàn)多圖上傳功能實例詳解,本文圖片加實例相結合的形式給大家介紹的非常詳細,需要的朋友可以參考下2018-04-04使用PHP?Smarty處理表單數(shù)據(jù)的方法
這篇文章主要介紹了如何使用PHP?Smarty處理表單數(shù)據(jù),首先需要下載Smarty庫并將其解壓到你的項目,下面通過本文結合實例代碼給大家講解的非常詳細,需要的朋友可以參考下2023-08-08