1

How do I programmatically use an external database?

$databases = array();
$databases['default']['default'] = array(
  // Drupal's default credentials here.
  // This is where the Drupal core will store its data.
);
$databases['my_other_db']['default'] = array(
  // Your secondary database's credentials here.
  // You will be able to explicitly connect to this database from your modules.
);
3
  1. Setup the external database on settings.php

  2. Select the correct database

    $con = \Drupal\Core\DatabaseDatabase::getConnection('default','my_other_db');
    

I took the example from http://www.drupal8.ovh/en/tutoriels/175/use-multiple-databases-drupal-8.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.