Multiple Instances

In some applications, you might need to use more than one database connection simultaneously. PDOdb supports isolated, named instances out of the box.

Creating an additional instance

To create a second connection that is completely independent of the default one, just pass the instance key to the constructor:

$db1 = new \decMuc\PDOdb\PDOdb([
  'host'     => 'localhost',
  'username' => 'your_user',
  'password' => 'your_password',
  'db'       => 'your_database',
  'charset'  => 'utf8mb4',
  'instance' => 'secondary'
  // 'prefix'  => 'client_' // optional: applies to all queries in this instance
]);

// Optional: override prefix manually
// $db1->setPrefix('client_'); // overrides any constructor value

If a prefix is set (either via constructor or setPrefix()), it will automatically apply to all table names used in queries, joins, and subqueries for this specific instance.

Important: Each named instance behaves like its own PDO connection – including transactions, locking and configuration.

Accessing a named instance

If you've created a named instance like above, you can later retrieve it using PDOdb::getInstance('secondary') from anywhere:

$db2 = \decMuc\PDOdb\PDOdb::getInstance('secondary');

This allows you to keep multiple database connections active and isolated at the same time.