1

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

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. I wanted to simply load multiple at the same time but my SQL + PHP knowledge is not that great

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

$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.

Andriy M
  • 76,112
  • 17
  • 94
  • 154
Russell
  • 13
  • 3
  • possible duplicate of [How do you connect to multiple MySQL databases on a single webpage?](http://stackoverflow.com/questions/274892/how-do-you-connect-to-multiple-mysql-databases-on-a-single-webpage) – AbcAeffchen Aug 15 '15 at 18:38
  • I think you have to use multiple connections. And you should use mysqli or PDO instead of mysql since the mysql-functions are deprecated and will be removed in PHP 7. – AbcAeffchen Aug 15 '15 at 18:40

1 Answers1

0

You can access tables of other database using 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";
Samir Selia
  • 7,007
  • 2
  • 11
  • 30