Read this link on how to connect to multiple dB's with Drupal: http://drupal.org/node/18429
Then...
You need first to add another database connection in sites/default/setting.php You will find your primary connection, and add another as stated in config file:
- Database configuration format:
- @code
- $databases['default']['default'] = array(
- 'driver' => 'mysql',
- 'database' => 'databasename',
- 'username' => 'username',
- 'password' => 'password',
- 'host' => 'localhost',
- 'prefix' => '',
- );
- $databases['default']['default'] = array(
- 'driver' => 'pgsql',
- 'database' => 'databasename',
- 'username' => 'username',
- 'password' => 'password',
- 'host' => 'localhost',
- 'prefix' => '',
- );
- $databases['default']['default'] = array(
- 'driver' => 'sqlite',
- 'database' => '/path/to/databasefilename',
- );
Then you can easily acess data from that another database, for example, executing script in node.tpl.php like this:
db_set_active('my_database');
$artikal = db_select('my_table', 'n')
->fields('n')
->condition('my_row', $my_variable, '=')
->execute()
->fetchAssoc();
and after acessing your other database, get back to default with
db_set_active('default');
db_set_active
5 database.inc db_set_active($name = 'default')
6 database.inc db_set_active($name = 'default')
7 database.inc db_set_active($key = 'default')
8 database.inc db_set_active($key = 'default')
Sets a new active database.
Parameters
$key: The key in the $databases array to set as the default database.
Return value
The key of the formerly active database.
Related topics
Database abstraction layer
Allow the use of different database servers using the same code base.
2 calls to db_set_active()
File
includes/database/database.inc, line 2522
Core systems for the database layer.
Code
function db_set_active($key = 'default') {
return Database::setActiveConnection($key);
}