How to choose multisite instance inside script running from main Drupal directory? 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';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
#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
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' =&gt; 'databasename',
      'username' =&gt; 'username', // assuming this is necessary
      'password' =&gt; 'password', // assuming this is necessary
      'host' =&gt; 'localhost', // assumes localhost
      'driver' =&gt; 'mysql', // replace with your database driver
  );
  // replace 'YourDatabaseKey' with something that's unique to your module
  Database::addConnectionInfo('YourDatabaseKey', 'default', $other_database);
  db_set_active('YourDatabaseKey');

  // 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.