Q: Wordpress - Establishing a database connection I am switching between two databases both are am my localhost (local machine) my script is:
# connect to 2nd db
$wpdb_backup = $wpdb;
$wpdb = new wpdb( Blog_DB_USER, Blog_DB_PASSWORD, Blog_DB_NAME, Blog_DB_HOST );
if (wp_set_wpdb_vars() )
{
    var_debug('Connection Fine');
}else{
    var_debug('Connection Fail');
}

# Re-connect to 1st db
$wpdb = $wpdb_backup;

both db has same credentials for (host_name, user_name & password) except db_name :)
It is working fine in another function but now if while trying to connect 2nd db browser says 

Error establishing a database connection

please guide what I am doing wrong.

A: I'd recommend leaving wpdb alone.
Try the following instead:
$wpdb_backup = new wpdb( Blog_DB_USER, Blog_DB_PASSWORD, Blog_DB_NAME, Blog_DB_HOST);
if ( is_object( $wpdb_backup ) && !isset($wpdb_backup->error) ) {
   var_debug('Connection Fine');
   # Do your processing
} else {
   var_debug('Connection Fail');
}

