How do I programmatically use an external database? 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.
);
Setup the external database on settings.php
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.