10

I'm developing a module that will rely heavily on external database queries. Is there a best practice for defining and using an external database connection throughout a module?

This page tells me how to make the connection, but not where to put it in a module (specific hook?) so I only have to define it once. Also, is it always necessary to do "db_set_active('YourDatabaseKey');" or can I pass an argument that will set the db to use? I'm using Drupal 7.

  • If you are using different types of databases you would need DBTNG module – Sivaji Oct 25 '11 at 4:21
  • Thanks for the suggestion, but that module is only necessary if you are using Drupal 6. I'm using Drupal 7. – Whiskey Oct 25 '11 at 13:33
10

There's no particular place to put this code (hook or module), you just put it where you need it.
So it should go just before your queries on the other database and just after to set back the default DB.

If all your module will rely on the external DB just put it a the beginning of the first function called for your module and at the end of the last function.

Of course every one of your function should be executed on the external DB and nothing must have to query the default database without switching back.

This following code would fail:

db_set_active('YourDatabaseKey');

$result = db_query('SELECT ...'); //Your own queries on the external DB.

$node = node_load(123); //This would fail on the external DB.

$result = db_query('SELECT ...'); //Your own queries on the external DB.

db_set_active(); 

You should switch back and forth:

db_set_active('YourDatabaseKey');

$result = db_query('SELECT ...'); //Your own queries on the external DB.

db_set_active(); 
$node = node_load(123); //Query made on the default Drupal DB.
db_set_active('YourDatabaseKey');

$result = db_query('SELECT ...'); //Your own queries on the external DB.

db_set_active(); 
  • 2
    Thanks for the explanation. I ended up creating a separate function that selects my external db with db_set_active and if that fails, it adds the db configuration and tries again. That way I can use it in the different hooks before running external queries, but I do need to set the default active again as you pointed out. – Whiskey Oct 25 '11 at 13:39
4

You will have to add into settings.php located at /sites/default/ in following syntax

//Drupal 6

$db_url['default'] = 'mysql://db_user:password@localhost/db_name';
$db_url['external'] = 'mysql://db_user2:password@localhost/db_name2';

//Drupal 7

$databases = array (
  'default' =>
  array (
    'default' =>
    array (
      'database' => 'db1',
      'username' => 'user1',
      'password' => 'pass',
      'host' => 'host1',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),
  'extra' =>
  array (
    'default' =>
    array (
      'database' => 'db2',
      'username' => 'user2',
      'password' => 'pass',
      'host' => 'host2',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),
);

And after these settings you can use db_set_active() to switch between databases.

  • Thanks, you can do it this way, but I opted to do it from within my module as described on the page I linked to in the question. That way you can have admin users fill in their own configuration on an admin setting form. – Whiskey Oct 25 '11 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.