ThinkPHP 3.1.x 串連多個資料庫使用不同字元編碼的方法,thinkphp3.1.x
因工作需要,某個項目需要用到ThinkPHP3.1.3進行開發。
因為項目有曆史原因,需要串連兩個字元編碼不同的資料庫,一個是UTF8,另一個LATIN1。
用過ThinkPHP的都知道,在/conf/config.php中,找到DB_CHARSET就能設定串連資料庫的字元編碼。
ThinkPHP預設的字元編碼為utf8,可以根據需要修改為LATIN1,GBK,等字元編碼。
'DB_CHARSET' => 'utf8'
因為這個項目用到兩個資料庫,主要資料庫的charset是utf8,在config.php中配置
//數據庫'DB_HOST'=>'localhost','DB_NAME'=>'main','DB_USER'=>'main','DB_PWD'=>'mainpassword',
另一個資料庫我在config.php中是使用下面的方式配置和調用的。
//資料庫配置2'OTHERDB_CONFIG' => array('db_type' => 'mysql','db_user' => 'other','db_pwd' => 'otherpassword','db_host' => '192.168.1.1','db_port' => '3306','db_name' => 'other', ),
調用方式
$member = M('member', null, 'OTHERDB_CONFIG');$data = $this->db(1, "OTHERDB_CONFIG")->query("select * from member order by addtime desc");
由於main DB需要使用utf8編碼,other DB需要使用LATIN1編碼,只有一個DB_CHARSET參數不能滿足。
然而我把OTHERDB_CONFIG修改加入db_charset也是無效。
//資料庫配置2'OTHERDB_CONFIG' => array('db_type' => 'mysql','db_user' => 'other','db_pwd' => 'otherpassword','db_host' => '192.168.1.1','db_port' => '3306','db_name' => 'other','db_charset' => 'LATIN1' // 加入無效 ),
難道ThinkPHP不支援串連多個資料庫使用不同的字元編碼嗎?
查看ThinkPHP源碼,找到關於資料庫的部分
ThinkPHP/Lib/Driver/Db/DbMysql.class.php
在function connect中找到一句,
mysql_query("SET NAMES '".C('DB_CHARSET')."'", $this->linkID[$linkNum]);
原來ThinkPHP是這樣設定串連資料庫的字元編碼的,居然是直接讀取C('DB_CHARSET')的值作為串連資料庫的字元編碼,並沒有提供其他參數設定。
connect 方法是可以傳入$config參數的,既然如此,就可以把charset通過$config參數傳入,然後修改DbMysql.class.php使其支援根據$config參數設定串連資料庫的字元編碼了。
因為DbMysql.class.php 是由Db.class.php 調用的,因此$config參數是在Db.class.php中傳入。
開啟ThinkPHP/Lib/Core/Db.class.php,找到function parseConfig。
$config參數的格式如下:
$db_config = array( 'dbms' => $db_config['db_type'], 'username' => $db_config['db_user'], 'password' => $db_config['db_pwd'], 'hostname' => $db_config['db_host'], 'hostport' => $db_config['db_port'], 'database' => $db_config['db_name'], 'dsn' => $db_config['db_dsn'], 'params' => $db_config['db_params'],);
因此,我們可以在config.php配置中加入db_params參數,設定db_charset了。
DbMysql.class.php 修改如下, 加入判斷$config['params]['db_charset']是否存在,如存在使用參數設定的db_charset否則使用C('DB_CHARSET')
/** * 串連資料庫方法 * @access public * @throws ThinkExecption */public function connect($config='',$linkNum=0,$force=false) { if ( !isset($this->linkID[$linkNum]) ) { if(empty($config)) $config = $this->config; // 處理不帶連接埠號碼的socket串連情況 $host = $config['hostname'].($config['hostport']?":{$config['hostport']}":''); // 是否長串連 $pconnect = !empty($config['params']['persist'])? $config['params']['persist']:$this->pconnect; if($pconnect) { $this->linkID[$linkNum] = mysql_pconnect( $host, $config['username'], $config['password'],131072); }else{ $this->linkID[$linkNum] = mysql_connect( $host, $config['username'], $config['password'],true,131072); } if ( !$this->linkID[$linkNum] || (!empty($config['database']) && !mysql_select_db($config['database'], $this->linkID[$linkNum])) ) { throw_exception(mysql_error()); } $dbVersion = mysql_get_server_info($this->linkID[$linkNum]); if(isset($config['params']['db_charset'])){ //使用指定編碼存取資料庫 mysql_query("SET NAMES '".$config['params']['db_charset']."'", $this->linkID[$linkNum]); }else{ //使用UTF8存取資料庫 mysql_query("SET NAMES '".C('DB_CHARSET')."'", $this->linkID[$linkNum]); } //設定 sql_model if($dbVersion >'5.0.1'){ mysql_query("SET sql_mode=''",$this->linkID[$linkNum]); } // 標記串連成功 $this->connected = true; // 登出資料庫連接配置資訊 if(1 != C('DB_DEPLOY_TYPE')) unset($this->config); } return $this->linkID[$linkNum];}
OTHERDB_CONFIG修改如下:
//資料庫配置2'OTHERDB_CONFIG' => array('db_type' => 'mysql','db_user' => 'other','db_pwd' => 'otherpassword','db_host' => '192.168.1.1','db_port' => '3306','db_name' => 'other','db_params' => array('db_charset' => 'LATIN1') ),
以後就可以通過db_params設定db_charset了。