My question is given the following code how can I use a single method to connect to multiple databases?
Array of config settings:
$GLOBALS['config'] = array(
'mysql' => array(
'host' => '127.0.0.1',
'username' => 'root',
'password' => '',
'db' => array(
'db1' => 'database1',
'db2' => 'database2'
)
)
Connect to database:
private function __construct() {
try {
$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' . Config::get('mysql/db/db1'), Config::get('mysql/username'), Config::get('mysql/password'));
} catch(PDOException $e) {
die($e->getMessage());
}
}
So instead of hard coding db1 into the connection:
dbname=' . Config::get('mysql/db/db1')
How to not specify a specific database here but allow for one to be called for later? I am trying to make a reusable class that can connect to any databases that are added to the config array.