In the settings.php file, $databases is an array composed of multiple database connection strings.
Here’s the default syntax, specifying a single connection:
array(
'driver' => 'mysql',
'database' => 'databasename',
'username' => 'username',
'password' => 'password',
'host' => 'localhost',
'port' => 3306,
'prefix' => 'myprefix_',
);
As an example, you might have two databases, the default database (in this case named D7) and a
legacy database as defined here.
$databases = array (
'default' =>
array (
'default' =>
array (
'driver' => 'mysql',
'database' => 'd7',
'username' => 'username',
'password' => 'userpassword',
'host' => 'localhost',
'port' => '',
'prefix' => '',
),
),
'legacy' =>
array (
'default' =>
array (
'driver' => 'mysql',
'database' => 'legacydatabase',
'username' => 'legacyusername',
'password' => 'legacyuserpassword',
'host' => '122.185.22.1',
'port' => '6060',
),
),
);
When you need to connect to one of the other databases in Drupal, you activate it by its key name
and switch back to the default connection when finished:
// Get some information from a non-Drupal database.
db_set_active('legacy');
$result = db_query("SELECT * FROM ldap_user WHERE uid = :uid", array(':uid' => $user->uid));
// Switch back to the default connection when finished.
db_set_active('default');
Here’s the default syntax, specifying a single connection:
array(
'driver' => 'mysql',
'database' => 'databasename',
'username' => 'username',
'password' => 'password',
'host' => 'localhost',
'port' => 3306,
'prefix' => 'myprefix_',
);
As an example, you might have two databases, the default database (in this case named D7) and a
legacy database as defined here.
$databases = array (
'default' =>
array (
'default' =>
array (
'driver' => 'mysql',
'database' => 'd7',
'username' => 'username',
'password' => 'userpassword',
'host' => 'localhost',
'port' => '',
'prefix' => '',
),
),
'legacy' =>
array (
'default' =>
array (
'driver' => 'mysql',
'database' => 'legacydatabase',
'username' => 'legacyusername',
'password' => 'legacyuserpassword',
'host' => '122.185.22.1',
'port' => '6060',
),
),
);
When you need to connect to one of the other databases in Drupal, you activate it by its key name
and switch back to the default connection when finished:
// Get some information from a non-Drupal database.
db_set_active('legacy');
$result = db_query("SELECT * FROM ldap_user WHERE uid = :uid", array(':uid' => $user->uid));
// Switch back to the default connection when finished.
db_set_active('default');
Comments
Post a Comment