EDITED
How to connect then fetch data from online database to drupal 8 on localhost. Both of them using postgre as main database. I have followed this tutorial and change settings.php
's external host with ip from my online database. I try to fetch data with my module, refer to this but still not work, my module look like this:
<?php
namespace Drupal\example\Controller;
class ExampleController {
public function example() {
\Drupal\Core\Database\Database::setActiveConnection('external');
// Get a connection going
$db = \Drupal\Core\Database\Database::getConnection();
print_r($db->databaseType);
$query = $db->select('users', 'u')
->fields('u', ['name', 'email']);
$result = $query->execute()->fetchAll();
print_r($result);
\Drupal\Core\Database\Database::setActiveConnection();
}
}
I was edited this code, looks more simple, and now showing result
pgsql The website encountered an unexpected error. Please try again later.
this result only showing result from print_r($db->databaseType);
but i still confuse, is this come from external database? Thank's for some help of this case.