[英]How to fetch the data for PHP from two databases on different hosts and put them in a single html table?
I have to put the data into a php page from the databases which are in seperate hosts. 我必须将数据从单独主机中的数据库放入php页面。 The one workaround i can think of is getting the data from both the databases onto an array and then displaying it. 我能想到的一种解决方法是将两个数据库中的数据都放入一个数组中,然后显示出来。 What if the array is multi dimensional? 如果数组是多维的怎么办? How to merge data from two databases on two different hosts in PHP? 如何在PHP中的两个不同主机上合并来自两个数据库的数据? I am using PHP5. 我正在使用PHP5。
Specifics: I am working on a booking engine. 细节:我正在开发一个预订引擎。 There are booking pages for each state and databases for each of them on different hosts.I am working on creating a kind of summary page for staff, which shows the bookings done for all the states in a single page. 每个州都有预订页面,每个州都有不同的主机数据库。我正在为员工创建一种摘要页面,该页面在一个页面中显示所有州的预订。 They need it for internal use. 他们需要内部使用。 The booking table in each of the database has same no.of rows. 每个数据库中的预订表具有相同的行数。 I need to know how merge the arrays obtained from these databases and then display it in a single web page. 我需要知道如何合并从这些数据库获得的数组,然后将其显示在单个网页中。
1) Connect to first database 1)连接到第一个数据库
2) Fetch and save the data in an array 2)提取数据并将其保存在数组中
3) Connect to second database 3)连接到第二个数据库
4) Fetch and save the data in the same array 4)提取数据并将其保存在同一数组中
5) Loop through the array and display results 5)遍历数组并显示结果
You can connect first database by 您可以通过以下方式连接第一个数据库
$link1 = mysql_connect('dbserver1', 'mysql_user1', 'mysql_password1');
and select
$db_selected = mysql_select_db('foo1', $link1);
and connect second database by 并通过连接第二个数据库
$link2 = mysql_connect('dbserver2', 'mysql_user2', 'mysql_password2');
and select
$db_selected = mysql_select_db('foo2', $link2);
and you can combine the result in to an array show it. 您可以将结果合并到一个数组中以显示它。
First of all you have not mentioned the Database name which you are using. 首先,您没有提到您正在使用的数据库名称。 I assume that it will be mySQL. 我假设它将是mySQL。 Now is/are there some relational fields...? 现在/是否有一些关系领域...?
Another way, though it might be vague, but is possible, is that you can populate a DUMMY table with Database1 table and then update the same table with Database2 table. 另一种方法,虽然可能含糊不清,但可能的是,可以用Database1表填充DUMMY表,然后用Database2表更新同一表。 Then you have both DB tables combined which is quite easy to display using PHP... You will need 2 connections, 2 queries for combining them.. 然后,您将两个数据库表组合在一起,使用PHP可以很容易地显示它们。您将需要2个连接,2个查询来组合它们。
Like conn1 for DB1 and conn2 for DB2. 就像conn1 for DB1和conn2 for DB2一样。
Also you ca use schema name if your DB allows. 如果您的数据库允许,您也可以使用架构名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.