3

As per the comments in settings.php:

/*
 * You can also use a reference to a schema/database as a prefix. This maybe
 * useful if your Drupal installation exists in a schema that is not the default
 * or you want to access several databases from the same code base at the same
 * time.
 * Example:
 * @code
 *   'prefix' => array(
 *     'default'   => 'main.',
 *     'users'     => 'shared.',
 *     'sessions'  => 'shared.',
 *     'role'      => 'shared.',
 *     'authmap'   => 'shared.',
 *   );
 * @endcode
 */

So if my current simple default looks like:

$databases = array (
  'default' => 
  array (
    'default' => 
    array (
      'database' => 'dev',
      'username' => 'dev',
      'password' => 'dev',
      'host' => 'localhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => false,
    ),
  ),
);

How would I add two tables (table1 and table2) on another database (dev1) on localhost so that they are available to drupal?

1 Answer 1

3

If they're on the same mysql host (and you've got the appropriate permissions) you can access the tables by simply prefixing them with the schema name: otherdatabasename.tablename. You can even run queries joining across the two databases.

If they're on a different server you'll need to add a separate connection and specify it as part of the query by using the $options['target'] setting.

4
  • "If they're on the same mysql host (and you've got the appropriate permissions) you can access the tables by simply prefixing them with the schema name: otherdb.tablename. You can even run queries joining across the two databases" - this is exactly what I want the outcome to be. But whats the correct format in settings.php that would do this? Do you have an example?
    – 7wonders
    Jan 24, 2012 at 18:10
  • if they're on the same host you don't need to do anything other than just refer to the table by it's full name, no new connection necessary.
    – drewish
    Jan 24, 2012 at 22:08
  • Im pretty sure I had tried that but perhaps I had done it in the wrong context. Will take another look and get back.
    – 7wonders
    Jan 26, 2012 at 21:04
  • cool, yeah post any error messages you get.
    – drewish
    Jan 27, 2012 at 4:55

Your Answer

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

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