Skip to content

Instantly share code, notes, and snippets.

@drugan
Last active October 17, 2023 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drugan/924b54674350d00ec3533525e1fb4eb9 to your computer and use it in GitHub Desktop.
Save drugan/924b54674350d00ec3533525e1fb4eb9 to your computer and use it in GitHub Desktop.
<?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);
<?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