Skip to content

Instantly share code, notes, and snippets.

@anxp
Created January 30, 2019 20:20
Show Gist options
  • Save anxp/36f7e4756235ab4a17670b492be1e699 to your computer and use it in GitHub Desktop.
Save anxp/36f7e4756235ab4a17670b492be1e699 to your computer and use it in GitHub Desktop.
Working with EXTERNAL database with Drupal 7 API - example.
<?php
//Array with connection credentials. This is the same structured array like in "settings.local.php" file in your Drupal set up.
$db_connection_data = [
'host' => 'localhost',
'database' => 'example_db',
'username' => 'root',
'password' => '',
'driver' => 'mysql',
];
Database::addConnectionInfo('temporary_db_key', 'default', $db_connection_data);
//Set active another\external database:
db_set_active('temporary_db_key');
/*
Do anything we want to do with DB via standard Drupal DB API here.
*/
//It's very important to revert active database to default!
db_set_active();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment