-
-
Save tim-peterson/3749863 to your computer and use it in GitHub Desktop.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class Readreplica { | |
public function Readreplica(){ | |
$config['hostname'] = "myReadReplica.com"; | |
$config['username'] = "xxxxxx"; | |
$config['password'] = "xxxxxx"; | |
$config['database'] = "myDB"; | |
$config['dbdriver'] = "mysqli"; | |
$config['port'] = 3306; | |
$config['dbprefix'] = ''; | |
$config['pconnect'] = TRUE; | |
//$config['db_debug'] = TRUE; | |
$config['db_debug'] = FALSE; | |
$config['cache_on'] = FALSE; | |
$config['cachedir'] = ''; | |
$config['char_set'] = 'utf8'; | |
$config['dbcollat'] = 'utf8_general_ci'; | |
$config['swap_pre'] = ''; | |
//$config['autoinit'] = TRUE; | |
$config['autoinit'] = FALSE; | |
$config['stricton'] = FALSE; | |
$config['ssl_set'] = true; | |
$config['ssl_key'] = NULL; | |
$config['ssl_cert'] = NULL; | |
$config['ssl_ca'] = realpath('./application/third_party/mysql-ssl-ca-cert.pem'); | |
$config['ssl_capath'] = NULL; | |
$config['ssl_cipher'] = NULL; | |
$CI =& get_instance(); | |
$rr=$CI->load->database($config, TRUE); | |
$connected = $rr->initialize(); | |
if(!$connected){ | |
$config['hostname'] = "myMasterDatabase.com"; | |
$rr=$CI->load->database($config, TRUE); | |
} | |
else{ | |
$rr=$CI->load->database($config, TRUE); | |
} | |
$this->CI =& get_instance(); | |
$this->load=$rr; | |
} | |
} |
whats the ideia, why not use autoload ?
I found this through this thread: https://stackoverflow.com/questions/12483861/codeigniter-php-check-if-can-connect-to-database#:~:text=Connecting%20manually%20to%20a%20database%20in%20codeigniter%20using,and%20throw%20that%20error%20for%20you.%20%E2%80%93%20Iber%C3%AA
The issue is finding out whether the db connected successfully (say, during the install process for your app) as using autoinit will return a DB object with empty connection data and connected to nothing. By initializing it yourself, the initialize function will return false without the connection data or with bad creds.
you could also fire off an innocuous query and check the error object:
if (! $db->simpleQuery('SELECT
example_fieldFROM
example_table')) { $error = $db->error(); // Has keys 'code' and 'message' }
how to use it ?