As in question. I know that I can use drush command, but I have small script and I don't want to create module from it. Now I calling that script inside main Drupal folder (no multisites).

Inside that script I just bootstraping Drupal (I need Access to Drupal API):

define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
#db integration

(The same PHP header I use for e.g unit tests class)

It is possible to run such script in multisite enviromnent too?

My problem is that every sites config settings.php has db name called 'default'. So I don't know how to use db_set_active properly.

I have script that creating data (txt file) for Menu Import module. I need access to Drupal and Joomla db, because I need to migrate menu from Joomla.

Anyway here is a script

  • Exactly what are you trying to do? Are you going to manipulate data in the database? Have you separate databases for each site? Please note that DRUPAL_ROOT is probably not set yet, unless you have included a file above that row.
    – hampusn
    Sep 12, 2012 at 6:56
  • Manipulating data. I have migrating script that uses Batch API. It slow, very slow. I would to use console, but I don't know want to rewrite it for using Drush. DRUPAL_ROOT is set. I edited my code.
    – Codium
    Sep 12, 2012 at 6:59
  • Then to answer your question, yes, you can run such script in a multisite environment. I don't know how the Batch API looks like, but depending on the number of databases you have, you'll wan't to take a few at a time. You would also have to specify which databases to connect to. Do you have a known list or do you know if every database at the server should be modified? If you have a known pattern for the drupal databases' names then you could use something like SHOW DATABASES LIKE '%drupal%' to get the relevant ones.
    – hampusn
    Sep 12, 2012 at 7:11
  • Yes I know db to modify. it's single db, but I must avoid to modify or others dbs.
    – Codium
    Sep 12, 2012 at 7:14
  • Then just connect to that one. I can't really understand what your problem is? Is there something that doesn't work?
    – hampusn
    Sep 12, 2012 at 7:15

1 Answer 1


Since you know the database name, you can dynamically set a database name which you will call with db_set_active().

This is copy/pasted from the link below.

  $other_database = array(
      'database' => 'databasename',
      'username' => 'username', // assuming this is necessary
      'password' => 'password', // assuming this is necessary
      'host' => 'localhost', // assumes localhost
      'driver' => 'mysql', // replace with your database driver
  // replace 'YourDatabaseKey' with something that's unique to your module
  Database::addConnectionInfo('YourDatabaseKey', 'default', $other_database);

  // execute queries here

  db_set_active(); // without the paramater means set back to the default for the site
  drupal_set_message(t('The queries have been made.'));

More about this can be found here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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