0

Is it possible to connect different databases located in different subdomains by using mysqli_connect, and then run a single select query to get data from all the databases? Suppose I have two databases located in different sub-domain:

$connect1 = mysqli_connect($hostname1, $username1, $password1,$database1); 
$connect2 = mysqli_connect($hostname2, $username2, $password2, $database2);

Then I want to select data from the two databases:

SELECT `Id` FROM `$database1`.`users` WHERE `name` LIKE '%$string_1%'
UNION ALL
SELECT `Id` FROM `$database2`.`masters` WHERE `name` LIKE '%$string_2%';

Here users is a database table of $database1 and masters is a table of $database2. But I don't know how to make a single mysqli_query with this.

Grvito
  • 13
  • 3
  • 1
    You can't union across servers unfortunately – ADyson Dec 21 '22 at 07:54
  • I'm afraid this is not possible. As you can see for mysqli_connect() method takes exactly four arguments including database name. You cannot include multiple values there. Can I suggest importing the second table to the first database or vice versa. – HARINDA VITHANA Dec 21 '22 at 07:58
  • *Someone who proposed to have these two tables on different hosts* (and even databases) must be assigned to this task. – Your Common Sense Dec 21 '22 at 08:09

0 Answers0