用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1853|回复: 2
收起左侧

[数据库] 切换数据库连接

[复制链接]
发表于 2018-5-28 13:21:54 | 显示全部楼层 |阅读模式
autoload里自动连接了数据库$autoload['libraries'] = array('database');exit;
pconnect为FALSE;
现在想连接另一个数据库.在控制器里添加$this->load->database('group_one', TRUE);报500错误.这是啥原因
发表于 2018-5-29 15:39:58 | 显示全部楼层
连接多个数据库请按照这里写的做 http://codeigniter.org.cn/user_guide/database/connecting.html#id6
发表于 2018-6-25 14:33:27 | 显示全部楼层
本帖最后由 飘扬过海 于 2018-6-25 14:35 编辑

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
        'dsn'        => '',
        
        'hostname' => '127.0.0.1:123',
        'username' => 'root',
        'password' => 'root',
        
        'database' => 'root',
        'dbdriver' => 'mysqli',
        'dbprefix' => 'pay_',
        'pconnect' => FALSE,
        'db_debug' => (ENVIRONMENT !== 'production'),
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt' => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array(),
        'save_queries' => TRUE
);

$db['support'] = array(
        'dsn'        => '',
        
        'hostname' => '127.0.0.1:234',
        'username' => 'root',
        'password' => 'root',
        'database' => 'root',
        'dbdriver' => 'mysqli',
        'dbprefix' => 'yang_',
        'pconnect' => FALSE,
        'db_debug' => (ENVIRONMENT !== 'production'),
        'cache_on' => FALSE,
        'cachedir' => '',
        'char_set' => 'utf8',
        'dbcollat' => 'utf8_general_ci',
        'swap_pre' => '',
        'encrypt'  => FALSE,
        'compress' => FALSE,
        'stricton' => FALSE,
        'failover' => array(),
        'save_queries' => TRUE
);
要使用那个链接在你的model里这样就行了:
        function __construct(){
                parent::__construct();
                $this->db = $this->load->database('support', true);
        }

本版积分规则