Created
January 30, 2019 20:20
-
-
Save anxp/36f7e4756235ab4a17670b492be1e699 to your computer and use it in GitHub Desktop.
Working with EXTERNAL database with Drupal 7 API - example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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