|
我是菜鸟,刚刚入手CI 2.0,发现我在实际工作中用到的多服务器方面的东西在这里没有被介绍到,所以就把自己刚学来的拿出来献丑,为的是让下一个菜鸟不在迷茫。
现在要解决的问题很简单,
1、我有两个数据库服务器,根据功能,姑且命名为“comment” 和 “favorite”,上面分别有同名数据库
2、CI目录的application/config/database.php内容如下:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
$active_group = 'comment';//作为默认
$active_record = TRUE;
//评论comment服务器
$db['comment'] = array(
'hostname' => '10.9.10.1:3306',
'username' => 'myname',
'password' => 'mypassword',
'database' => 'comment',
'dbdriver' => 'mysql',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'gbk',
'dbcollat' => 'gbk_chinese_ci',
'swap_pre' => '',
'autoinit' => TRUE,
'stricton' => FALSE,
);
//收藏服务器
$db['favorite'] = array(
'hostname' => '10.9.10.5:3306',
'username' => 'myusername',
'password' => 'mypassword',
'database' => 'favorite',
'dbdriver' => 'mysql',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'gbk',
'dbcollat' => 'gbk_chinese_ci',
'swap_pre' => '',
'autoinit' => TRUE,
'stricton' => FALSE,
);
/* End of file database.php */
/* Location: ./application/config/database.php */
3、下面示范用法在application/controllers/mytest.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Mytest extends CI_Controller {
public function index() {
$this->load->database('comment', TRUE);//comment是默认数据库,这里也可以省略参数
$query = $this->db->get('commenttable001', 10);
var_dump($query->result());
$DBFavorite = $this->load->database('favorite', TRUE);
$query = $DBFavorite->get('favorite001', 10);
var_dump($query->result());
}
} |
|