连接、查询和同时使用2个或更多数据库

ztyzrc3y  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(305)

我试图打开与4个数据库(同一主机)的连接。四个看起来都一样。
这是我的代码,但没有输出。为什么?有人有办法吗?也许我的问题不对?
谢谢您!

<?php 

 $db = new mysqli("localhost","username","password");

 mysqli_select_db('database1', $db); 
 mysqli_select_db('database2', $db); 
 mysqli_select_db('database3', $db); 
 mysqli_select_db('database4', $db); 

 $query = "SELECT A.name AS name1, 
                  B.name AS name2, 
                  C.name AS name3, 
                  D.name AS name4 
        FROM database1.table1 A, 
             database2.table2 B, 
             database3.table3 C, 
             database4.table4 D"; 
 $result = $db->query($query);

 while($row = $result->fetch_assoc()) {

 echo $row['name1'];
 echo $row['name2'];
 echo $row['name3'];
 echo $row['name4'];
 } 

?>
bis0qfac

bis0qfac1#

好 啊。我有我的解决办法。一个连接,只有查询!谢谢你们的提示!

$query = "
SELECT database1.table1.name as name FROM database1.table1
UNION ALL
SELECT database2.table2.name as name FROM database2.table2
UNION ALL
SELECT database3.table3.name as name FROM database3.table3
UNION ALL
SELECT database4.table4.name as name FROM database4.table4 order by name";

相关问题