Drupal 7 installed and working fine with the database named "DrupalDB". The another database "CustomDB" contains one table. how to connect "CustomDB" in drupal 7?
1 Answer
you can see the settings.php file(the place db information is stored) in where the Drupal 7 installation.in there you can see a array like this.
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'db-name',
'username' => 'db-username',
'password' => 'db-password',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
);
To allow modules to quickly make connections to other databases, you’ll need to add additional information to the $databases array:
$databases['CustomDB']['default'] = array (
'database' => 'CustomDB',
'username' => 'CustomDB-username',
'password' => 'CustomDB-password',
'host' => 'localhost',
'driver' => 'mysql',
);
You can see, this code defines another database identified by the array key CustomDB. So when you need to query this other database in your modules, you have to switch the connection to it with this function:
db_set_active('CustomDB');
the,you have to close it and revert back to the default database connection in order for Drupal to be able to access its data
db_set_active();
As you don’t pass a parameter to function db_set_active(), it will switch back to the default database