0% found this document useful (0 votes)
36 views

Multiple Database

To connect Drupal to multiple databases, you need to set the $db_url variable in settings.php as an array with keys for each database name and the connection details. You can then query a different database by calling db_set_active() and specifying the database key, and switch back to the default by calling db_set_active() again with the 'default' key. The document provides an example $db_url array with keys for 'default', 'mydb', and 'db3' databases and shows how to query the 'mydb' database before switching back.

Uploaded by

Denny England
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Multiple Database

To connect Drupal to multiple databases, you need to set the $db_url variable in settings.php as an array with keys for each database name and the connection details. You can then query a different database by calling db_set_active() and specifying the database key, and switch back to the default by calling db_set_active() again with the 'default' key. The document provides an example $db_url array with keys for 'default', 'mydb', and 'db3' databases and shows how to query the 'mydb' database before switching back.

Uploaded by

Denny England
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Drupal Module Development

How to connect to multiple databases within Drupal


You need to set the $db_url variable in the
settings.php file for the site. Example:
//
mysql://username[:password]@hostname[:port]/databasen ame $db_url = 'mysql://root@localhost/testdrupal';

Cont..
To allow multiple database connections, convert $db_url to an array.
<?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'; ?>

Cont..
To query a different database, simply set it as active by referencing the key name.
<?php db_set_active('mydb'); db_query('SELECT * FROM table_in_anotherdb'); //Switch back to the default connection when finished. db_set_active('default');

?>

Drupal Module Development

ICT Center
We are making IT happen

Integrity

Commitment

Teamwork

You might also like