简体   繁体   中英

How to switch between many databases without having to manually execute lots of USE DB queries

I have a PHP script that reads and writes from multiple databases. I am using the mysqli extension. I connect to servers like this:

$link = new mysqli( $host, $user, $passwd );

The problem is that in my code I have to keep doing this when I want to read/write from a different database:

$this->link->select_db( 'some_db' );

but it's a pain having to remember to select the DB before using it.

I could do this:

$link1 = new mysqli( $host, $user, $passwd, $db1 );
$link2 = new mysqli( $host, $user, $passwd, $db2 );
...
...

but why make many connections to a host when I should only need one? Even though there are multiple DBs.

Is there a way you're meant to do this?

Thank you :).

Use 2-part object names?

SELECT * FROM db1.SomeTable;
CALL db2.SomeProc();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM