how to connection error handling in codeigniter php

125

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?

690

Answer

Solution:

Since you want to know the response to the intialize, I’d recommend turning off auto initialize toFALSE 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;

People are also looking for solutions to the problem: php - Upload Multiple file drag drop codeigniter with multiple category

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.