Last active
October 17, 2023 08:14
-
-
Save drugan/924b54674350d00ec3533525e1fb4eb9 to your computer and use it in GitHub Desktop.
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 | |
use Drupal\Core\Database\Database; | |
// Method one. | |
$result = \Drupal::database()->query('SELECT * FROM my_additional_db.my_table')->fetchAllAssoc('my_column'); | |
dump($result); | |
// Method two. | |
$conn = Database::setActiveConnection('my_extra_connection'); | |
$conn = Database::getConnection(); | |
// Execute any query against your additional database. | |
$table_names = $conn->schema()->findTables('%'); | |
dump($table_names); | |
$result = \Drupal::database()->query('SELECT * FROM my_table')->fetchAllAssoc('my_column'); | |
dump($result); |
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 | |
// ... | |
// Append additional database info to the Drupal settings.php file. | |
$databases['extra_connection']['default'] = [ | |
'driver' => 'mysql', | |
'database' => 'my_additional_db', | |
'username' => 'my_additional_db_USER', | |
'password' => 'my_additional_db_USER_PASSWORD', | |
'host' => 'my_additional_db_host', // Might be a localhost or external host. | |
'port' => '3306', // For the most db hosts the default 3306 port should work. | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment