如何在Codeigniter数据库配置中添加两个数据库连接(本地主机,Web服务器)?

时间:2019-06-29 03:20:09

标签: php codeigniter frameworks

我只想知道如何在CG(Database Config)中建立两个活动连接,一个是Localhost,另一个是Web。我想有两个数据库连接来保证故障安全。我还没有尝试对其进行编码,但是一个Idea或一个示例代码将有很大的帮助。 (如何添加网络服务器连接?)

这是单个连接(本地):

                  $active_group = 'default';
                  $query_builder = TRUE;
                  $db['default'] = array('dsn'  => '',
                  'hostname' => 'localhost',
                  'username' => 'root',
                  'password' => '',
                  'database' => 'projectbox_db',
                  'dbdriver' => 'mysqli',
                  'dbprefix' => '',
                  'pconnect' => FALSE,
                  'db_debug' => TRUE,
                  'cache_on' => FALSE,
                  'cachedir' => '',
                  'char_set' => 'utf8',
                  'dbcollat' => 'utf8_general_ci',
                  'swap_pre' => '',
                  'encrypt' => FALSE,
                  'compress' => FALSE,
                  'stricton' => FALSE,
                  'failover' => array(),
                  'save_queries' => TRUE);

1 个答案:

答案 0 :(得分:0)

if(ENVIRONMENT == 'production') {
 $active_group = 'default';
                  $query_builder = TRUE;
                  $db['default'] = array('dsn'  => '',
                  'hostname' => 'localhost',
                  'username' => 'server_username',
                  'password' => 'server_password',
                  'database' => 'projectbox_db',
                  'dbdriver' => 'mysqli',
                  'dbprefix' => '',
                  'pconnect' => FALSE,
                  'db_debug' => TRUE,
                  'cache_on' => FALSE,
                  'cachedir' => '',
                  'char_set' => 'utf8',
                  'dbcollat' => 'utf8_general_ci',
                  'swap_pre' => '',
                  'encrypt' => FALSE,
                  'compress' => FALSE,
                  'stricton' => FALSE,
                  'failover' => array(),
                  'save_queries' => TRUE);
} else {
 $active_group = 'default';
                  $query_builder = TRUE;
                  $db['default'] = array('dsn'  => '',
                  'hostname' => 'localhost',
                  'username' => 'root',
                  'password' => '',
                  'database' => 'projectbox_db',
                  'dbdriver' => 'mysqli',
                  'dbprefix' => '',
                  'pconnect' => FALSE,
                  'db_debug' => TRUE,
                  'cache_on' => FALSE,
                  'cachedir' => '',
                  'char_set' => 'utf8',
                  'dbcollat' => 'utf8_general_ci',
                  'swap_pre' => '',
                  'encrypt' => FALSE,
                  'compress' => FALSE,
                  'stricton' => FALSE,
                  'failover' => array(),
                  'save_queries' => TRUE);  
}

您必须根据本地代码和生产代码来更改环境