[英]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. 我有一个PHP
脚本,可以从多个数据库中读取和写入。 I am using the mysqli
extension. 我正在使用mysqli
扩展。 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. 即使有多个DB。
Is there a way you're meant to do this? 你有没有办法做到这一点?
Thank you :). 谢谢 :)。
Use 2-part object names? 使用两部分对象名称?
SELECT * FROM db1.SomeTable;
CALL db2.SomeProc();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.