1

I have two server and try to connect either first or second, If first connection alive than use first connection id else connect to another server, But connection id not comes?

Here is my config/database.php

$db['slave01']['hostname'] = 'xx.xx.xxx.xx';
$db['slave01']['username'] = 'xxxxxx';
$db['slave01']['password'] = 'xxxx';
$db['slave01']['database'] = 'xxx';
$db['slave01']['dbdriver'] = 'mysql';
$db['slave01']['dbprefix'] = '';
$db['slave01']['pconnect'] = TRUE;
$db['slave01']['db_debug'] = FALSE;   // =>here set FALSE
$db['slave01']['cache_on'] = FALSE;
$db['slave01']['cachedir'] = '';
$db['slave01']['char_set'] = 'utf8';
$db['slave01']['dbcollat'] = 'utf8_general_ci';
$db['slave01']['swap_pre'] = '';
$db['slave01']['autoinit'] = TRUE;
$db['slave01']['stricton'] = FALSE;

and another server details

    $db['slave02']['hostname'] = 'xx.xx.xxx.xx';
    $db['slave02']['username'] = 'xxxxxx';
    $db['slave02']['password'] = 'xxxx';
    $db['slave02']['database'] = 'xxx';
    $db['slave02']['dbdriver'] = 'mysql';
    $db['slave02']['dbprefix'] = '';
    $db['slave02']['pconnect'] = TRUE;
    $db['slave02']['db_debug'] = FALSE; // =>here set FALSE
    $db['slave02']['cache_on'] = FALSE;
    $db['slave02']['cachedir'] = '';
    $db['slave02']['char_set'] = 'utf8';
    $db['slave02']['dbcollat'] = 'utf8_general_ci';
    $db['slave02']['swap_pre'] = '';
    $db['slave02']['autoinit'] = TRUE;
    $db['slave02']['stricton'] = FALSE;

in controller

    $this->READ = $this->load->database('slave01', TRUE);
    if(!$this->READ->conn_id){
        echo "<br>Not connected";
        $this->READ = $this->load->database('slave02', TRUE);
    }else{
        echo "<br>Connected slave01 : ".$this->READ->conn_id;
    }

    echo "<br>Connected slave02 : ".$this->READ->conn_id;

But connection id not print! how can i resolved?

1
  • Worth question actually!! Commented Sep 10, 2014 at 5:52

1 Answer 1

1

Since you want to know the response to the intialize, I’d recommend turning off auto initialize to FALSE of both the db connections.

$db['slave01']['autoinit'] = FALSE;
// AND
$db['slave02']['autoinit'] = FALSE;


Then in your code that checks the db state, check TRUE/FALSE of the initialize() function:

$db_obj = $this->load->database('slave01',TRUE);
$connected = $db_obj->initialize();
if (!$connected) 
{
    $db_obj = $this->load->database('slave02',TRUE);
}

$db_obj->conn_id = ($db_obj->pconnect == FALSE) ? $db_obj->db_connect() : $db_obj->db_pconnect();
echo $db_obj->conn_id;
Sign up to request clarification or add additional context in comments.

2 Comments

after following condition I'm try to print echo "<br>Connected : ".$db_obj->conn_id; than output not comes?
This is valid in codegniter version 2, as they remove it in version 3, check the link of changelog : codeigniter.com/user_guide/changelog.html?highlight=autoinit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.