Codeigniter - 多个数据库连接
我必须从 master 数据库中检索 MySQL 数据库信息,然后连接到该数据库,并获取一些记录.
I have to retrieve a MySQL database information from master database and then connect to that database, and fetch some records.
我的意思是持有一个数据库我想加载另一个数据库.
I mean that holding one database I want to load another database.
使用 Codeigniter 可以吗?现在我在我的模型中使用以下代码行.
Is it possible with Codeigniter? Right now I'm using following lines of code in my model.
function connectDb($credential)
{
$config['hostname'] = $credential['server'];
$config['username'] = $credential['username'];
$config['password'] = $credential['password'];
$config['database'] = $credential['database'];
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
$DB2=$this->load->database($config);
$DB2->db->select('first_name,last_name');
$query = $DB2->db->get('person');
print_r($query);
}
它不起作用还有其他方法吗?
its not working is there any other way?
推荐答案
你应该在`application/config/database.php´
You should provide the second database information in `application/config/database.php´
通常,您会设置default
数据库组,如下所示:
Normally, you would set the default
database group, like so:
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "database_name";
$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;
请注意,登录信息和设置在名为 $db['default']
的数组中提供.
Notice that the login information and settings are provided in the array named $db['default']
.
然后您可以在新数组中添加另一个数据库 - 我们称之为otherdb".
You can then add another database in a new array - let's call it 'otherdb'.
$db['otherdb']['hostname'] = "localhost";
$db['otherdb']['username'] = "root";
$db['otherdb']['password'] = "";
$db['otherdb']['database'] = "other_database_name";
$db['otherdb']['dbdriver'] = "mysql";
$db['otherdb']['dbprefix'] = "";
$db['otherdb']['pconnect'] = TRUE;
$db['otherdb']['db_debug'] = FALSE;
$db['otherdb']['cache_on'] = FALSE;
$db['otherdb']['cachedir'] = "";
$db['otherdb']['char_set'] = "utf8";
$db['otherdb']['dbcollat'] = "utf8_general_ci";
$db['otherdb']['swap_pre'] = "";
$db['otherdb']['autoinit'] = TRUE;
$db['otherdb']['stricton'] = FALSE;
现在,要实际使用第二个数据库,您必须将连接发送到可以在模型中使用的另一个变量:
Now, to actually use the second database, you have to send the connection to another variabel that you can use in your model:
function my_model_method()
{
$otherdb = $this->load->database('otherdb', TRUE); // the TRUE paramater tells CI that you'd like to return the database object.
$query = $otherdb->select('first_name, last_name')->get('person');
var_dump($query);
}
应该可以.可以在此处找到连接多个数据库的文档:http://codeigniter.com/user_guide/database/connecting.html
That should do it. The documentation for connecting to multiple databases can be found here: http://codeigniter.com/user_guide/database/connecting.html
相关文章