简体   繁体   English

如何在许多数据库之间切换而无需手动执行大量USE数据库查询

[英]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.

相关问题 有大量数据库的问题吗? - Problem having lots of databases? 如何使用Laravel Eloquent在不同服务器上的多个数据库之间执行Join查询? - How to execute Join queries between multiple Databases that are on different server with Laravel Eloquent? 如何在不加载新页面的情况下在变量集之间切换? - How to switch between variable sets without having to load a new page? Laravel-如何在数据库之间动态切换 - Laravel - How to switch between databases dynamically 学说-多对一; 填充很多,不能在数据库上使用常量“ 1”。 如何手动插入FK? - Doctrine - many to one; Populating many, can't use the constant “ones” on the DB. How to insert manually the FKs? 数据库之间的Codeigniter切换 - Codeigniter switch between databases 如何使用PHP执行许多mysql查询 - How to execute many mysql queries with PHP 如何在所有具有“正在使用”表的数据库中搜索 - How to search in all databases having `in use` table 在PHP中使用数组而不是大量的数据库查询 - Using array instead of lots of db queries in PHP 如果其中之一未连接,如何在2个mysql数据库之间切换? - How to switch between 2 mysql databases if one of them was not connected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM