简体   繁体   English

加载多个SQL数据库以进行查询

[英]Loading multiple SQL databases for a query

The background here is that we use a SQL to run a diary and the following commands bring up the results from one specific SQL 这里的背景是我们使用SQL来运行日记,并且以下命令显示来自一个特定SQL的结果

if ($_SERVER['HTTP_HOST'] == "localhost") {
    $GLOBALS['dbUsername']     = "admin";
    $GLOBALS['dbPassword']     = "admin";
    $GLOBALS['dbHost']         = "localhost";
    $GLOBALS['dbDatabase']     = "Tracker";
}
else {
    $GLOBALS['dbUsername']     = "admin";
    $GLOBALS['dbPassword']     = "admin";
    $GLOBALS['dbHost']         = "localhost";
    $GLOBALS['dbDatabase']     = "store_3";
}

However we have around 26 of these using "store_1" - "store_26" So to look through them all can take a while. 但是,我们大约有26个使用"store_1" - "store_26"因此要仔细检查它们都需要一段时间。 I wanted to simply load multiple at the same time but my SQL + PHP knowledge is not that great 我想同时简单地加载多个,但是我的SQL + PHP知识不是那么好

I thought that: 我认为:

$GLOBALS['dbDatabase']     = "store_3";"store_4"

Would allow me to review more than 1 but this fails. 请允许我检查多个,但失败了。 The only other reference to dbDatabase is slightly later in the code dbDatabase的唯一其他引用在代码中稍稍晚一点

$conn = mysql_connect($GLOBALS['dbHost'], $GLOBALS['dbUsername'], $GLOBALS['dbPassword']);
$data = mysql_select_db($GLOBALS['dbDatabase']);

So I am presuming if I log into multiple databases at the beginning I would also need a way to include them in this statement, any help would be much appreciated. 因此,我假设如果一开始登录多个数据库,那么我还需要一种将它们包含在此语句中的方法,我们将不胜感激。

You can access tables of other database using dbname.tablename 您可以使用dbname.tablename访问其他数据库的表

But the databases you want to access should be on same server. 但是您要访问的数据库应该在同一服务器上。

For example: 例如:

// Establish connection
$conn = mysql_connect($GLOBALS['dbHost'], $GLOBALS['dbUsername'], $GLOBALS['dbPassword']);

// Select Any one database. Let's say `store_1`
$data = mysql_select_db($GLOBALS['dbDatabase']);

$sql1 = "SELECT * FROM store_1.tablename";
$sql2 = "SELECT * FROM store_2.tablename";
$sql3 = "SELECT * FROM store_4.tablename a JOIN store_5.tablename b ON a.id = b.id";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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