Welcome Guest, Not a member yet? Register   Sign In
How to identify connection problems on 2nd database
#1

[eluser]billmce[/eluser]
I've got a situation where I have to open a 2nd database outside the active records.
This database is outside of my control and has, on occassion, been inactivated.
I need to be able to trap when the connection is down .. so I can react accordingly.
Code:
// Connect to the database.
$dsn = $this->__connectDB1();
$DB1 = $this->load->database($dsn, TRUE);

// Get the general headings.
$query = $DB1->get('general');

.... stuff happens here ....


function __connectDB1()
{
    /**
     * Connect to outside database
     *
     */
    $dsn['hostname'] = $this->config->item('stats_host','company_stats');
    $dsn['username'] = $this->config->item('stats_user','company_stats');
    $dsn['password'] = $this->config->item('stats_pass','company_stats');
    $dsn['database'] = $this->config->item('stats_db','company_stats');
    $dsn['dbdriver'] = "mysql";
    $dsn['dbprefix'] = "";
    $dsn['pconnect'] = FALSE;
    $dsn['db_debug'] = FALSE;
    $dsn['cache_on'] = FALSE;
    $dsn['cachedir'] = "";

    $dsn['char_set'] = "utf8";
    $dsn['dbcollat'] = "utf8_general_ci";
    return $dsn;

}

How can I identify when the database is not available?
Thanks in advance.
#2

[eluser]danmontgomery[/eluser]
You'd need to check $DB1->conn_id, which will be the resource ID on a successful connection and FALSE on failure.
#3

[eluser]billmce[/eluser]
That's what I was missing ... thanks.




Theme © iAndrew 2016 - Forum software by © MyBB