Multiple concurrent database connections in drupal 7

12,070

Solution 1

Typical. 5 minutes after posting i figure it out... so, for future googlers:

Basically, you don't use db_query, instead you run the query on your connection without setting the active link.

you can figure this out by looking at how db_query works: http://api.drupal.org/api/drupal/includes--database--database.inc/function/db_query/7

So it looks like this:

$target='default';
$key = 'phpbb';
$phpbb = Database::getConnection($target,$key);
$result = $phpbb->query($sql,$args,$opts);

This assumes you have a database configured in your settings.php like the following:

$databases['phpbb']['default'] = array(
    'driver' => 'mysql',
    'database' => 'forum',
    'username' => 'username',
    'password' => 'password',
    'host' => 'mysql.host.com',
    'prefix' => 'phpbb3_'
);

Solution 2

Database::addConnectionInfo() perhaps?

This method allows the addition of new connection credentials at runtime. Under normal circumstances the preferred way to specify database credentials is via settings.php. However, this method allows them to be added at arbitrary times, such as during unit tests, when connecting to admin-defined third party databases, etc.

If the given key/target pair already exists, this method will be ignored.

Share:
12,070

Related videos on Youtube

thtas
Author by

thtas

Updated on June 04, 2022

Comments

  • thtas
    thtas almost 2 years

    I'm writing a wrapper class for my drupal 7 site which lets me connect to and query my phpbb database.

    When connecting to an external data source (as per drupal documentation) you have set the active db, run the query, then set the active db back to the default.

    e.g.

    db_set_active('phpbb');
    $result = db_query($sql,$args,$opts);                               
    db_set_active();//back to default
    

    But is there any way to use drupal's database wrapper to create a brand new connection which can be permanently set to the new database without having to do this switching back-and-forth nonsense? surely we can handle connections to multiple databases concurrently.

    I have done some googling but haven't found anybody trying to do this as yet.

  • sillygwailo
    sillygwailo about 13 years
    drupal.org/node/18429 also has some code examples, especially if you want to configure the database settings dynamically, that is, not hard-coded in settings.php. For example, if you stored database settings somewhere other than in settings.php, you can dynamically create the array and use Database::addConnectionInfo() to make the database connection.
  • chx
    chx about 13 years
    $phpbb = Database::getConnection($target='default', key='phpbb'); this is an error which I see from time to time and I am at a loss how can people arrive to such a code. What you wanted is $phpbb = Database::getConnection('default', 'phpbb'); the only reason your code worked is that the expression $target = 'default' evaluates to default however the variable $target in the caller scope changes to 'default' which is probably not indended.
  • chx
    chx over 12 years
    Sigh.You still wanted $phpbb = Database::getConnection('default', 'phpbb');. Those variables are still unnecessary.

Related