我正在开发像公平规模的项目,它正处于开发阶段,使用MySQL作为主数据库。现在,客户端有一些数据,这是postgres SQL,无法转换为MySQL。所以,正如我所知,如果两个数据库的dbdriver相同,就好像两个数据库都是MySQL,那么我就能连接它。但问题是,有两种不同的数据库管理系统具有不同的DB驱动程序。那么如何同时连接两个数据库呢? 在我的database.php文件中,代码是:
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'xxx';
$db['default']['password'] = 'xxxxx';
$db['default']['database'] = 'xxxx';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
// ************************ anotherDatabase Connect *****************************************************
$db['database2']['hostname'] = 'xxx.xxx.xxx.xxx'; //ip address of the remote database
$db['database2']['username'] = 'xxxx';
$db['database2']['password'] = 'xxxx';
$db['database2']['database'] = 'xxxx';
$db['database2']['dbdriver'] = 'postgre';
$db['database2']['dbprefix'] = '';
$db['database2']['pconnect'] = TRUE;
$db['database2']['db_debug'] = TRUE;
$db['database2']['cache_on'] = FALSE;
$db['database2']['cachedir'] = '';
$db['database2']['char_set'] = 'utf8';
$db['database2']['dbcollat'] = 'utf8_general_ci';
$db['database2']['swap_pre'] = '';
$db['database2']['autoinit'] = TRUE;
$db['database2']['stricton'] = FALSE;
$db['database2']['port'] = 5433;
我过去一周一直在寻找这个并经历了很多帖子,但没有明确的相关信息。为了测试它,我编写了以下代码。
class C_test extends CI_Controller {
public function index()
{
echo "Test";
//$this->db->close();
//$db1 = $this->load->database('default',true);
$this->db2 = $this->load->database('database2',true);
echo "databasa Test ";
// $test1 = $db1->get('table_name')->result_array();
//print_r($test1);
//$this->db->close();
echo "=====================================================";
$test = $this->db2->get('table_name')->result_array();
//$test = $db2->get('admin')->result_array();
print_r($test);
}
}
答案 0 :(得分:1)
首先在php.ini中启用Postgresql扩展
extension=php_pgsql.dll
您也可以为PDO启用Postgresql扩展。
extension=php_pdo_pgsql.dll
然后,
$db['database2']['hostname'] = 'localhost';
$db['database2']['username'] = 'postgres';
$db['database2']['password'] = 'postgres';
$db['database2']['database'] = 'abc_gis';
$db['database2']['dbdriver'] = 'postgre';
$db['database2']['dbprefix'] = '';
$db['database2']['pconnect'] = TRUE;
$db['database2']['db_debug'] = TRUE;
$db['database2']['cache_on'] = FALSE;
$db['database2']['cachedir'] = '';
$db['database2']['char_set'] = 'utf8';
$db['database2']['dbcollat'] = 'utf8_general_ci';
$db['database2']['swap_pre'] = '';
$db['database2']['autoinit'] = TRUE;
$db['database2']['stricton'] = FALSE;
$db['database2']['port'] = 5432; // In question 5433 is written!
要解决此问题,请更改/system/database/DB_driver.php中的simple_query函数:
function simple_query($sql)
{
if ( ! $this->conn_id)
{
$this->initialize();
}
$this->db_select(); //<----------------- Added this line
return $this->_execute($sql);
}
这完全解决了这个问题,所以你可以在模型中做这样的事情
$this->db2 = $this->load->database('database2',true);