How to connect to multiple Databases in Drupal 7?
Two steps:


Add database credentials to the $databases array in settings.php file. You can have as many additional databases as you want. Example code:

$databases = array (
  'default' =&gt; 
  array (
    'default' =&gt; 
    array (
      'database' =&gt; 'YOUR_DEFAULT_DATABASE',
      'username' =&gt; 'username',
      'password' =&gt; 'password',
      'host' =&gt; 'localhost',
      'port' =&gt; '',
      'driver' =&gt; 'mysql',
      'prefix' =&gt; '',
    ),
  ),
  'your_second_db_alias' =&gt; 
  array (
    'default' =&gt; 
    array (
      'database' =&gt; 'YOUR_SECOND_DB',
      'username' =&gt; 'username',
      'password' =&gt; 'password',
      'host' =&gt; 'localhost',
      'port' =&gt; '',
      'driver' =&gt; 'mysql',
      'prefix' =&gt; '',
    ),
  ),
);

Use the following code to change the database and run queries. Note that you must switch back to the original database afterwards.

// Change active database.
db_set_active('your_second_db_alias');
// Run queries.
$query = db_query("SELECT * FROM yourtable");
// IMPORTANT: return current active database to the default one. If you
// don't do this, everything after that point will be executed against the
// above database, and cause errors most probably. If you are connecting
// multiple Drupal installations, you can cause a pretty big mess if data is
// partially saved in the other database.
// Always double check that you have this line!
db_set_active();



Hope this helps!