Multiple databases in Drupal
Have to say, while MDB2 has the more straightforward way of accessing multiple databases (just create more database objects using MDB2::factory()), drupal’s way of doing things is a long way from horrible.
To initialise:
[ccN lang=”php”]$db_url[‘default’] = ‘mysql://drupal:drupal@localhost/drupal’;
$db_url[‘mydb’] = ‘mysql://user:pwd@localhost/anotherdb’;
$db_url[‘db3’] = ‘mysql://user:pwd@localhost/yetanotherdb’;[/cc]
And then to use:
[ccN lang=”php”]db_set_active(‘mydb’);
db_query(‘SELECT * FROM table_in_anotherdb’);
//Switch back to the default connection when finished.
db_set_active(‘default’);[/cc]
Quite straightfoward looking. Now to see if it actually works! 🙂… Read the rest