Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple database connections #EnhancementRequest #5

Open
klor opened this issue Mar 21, 2016 · 0 comments
Open

Multiple database connections #EnhancementRequest #5

klor opened this issue Mar 21, 2016 · 0 comments

Comments

@klor
Copy link

klor commented Mar 21, 2016

Say I have a MySQL database with main contents for the site and a secondary SQLite database with project-specific contents.

I want to be able to create a separate database connection for each database.

Currently this is not possible because the instance() method only takes one value (Line 36):

self::$instance = new PDO($dsn, DB_USER, DB_PASS, $opt);

The request could be resolved by introducing a $dbConnectionName variable that (optionally) allow programmers to name each connection. To support this, instance() can name the connection like so:

$dbConnectionName = isset($dbConnectionName) ? $dbConnectionName : 'default';
self::$instance[$dbConnectionName] = new PDO($dsn, DB_USER, DB_PASS, $opt);

Programmers can now maintain connections to multiple databases, using code like this:

// Query DB1
$sql  = 'SELECT return, fields  FROM table WHERE search_field = ?';
$data = DB('db1'):prepare($sql)->execute([$search_val])->fetchAll();

// Query DB2
$sql  = 'SELECT return, fields  FROM table WHERE search_field = ?';
$data = DB('db2'):prepare($sql)->execute([$search_val])->fetchAll();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant