简体   繁体   English

如何从不同主机上的两个数据库中获取PHP的数据并将它们放在单个html表中?

[英]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.

相关问题 使用PHP将数据从MySQL和Postgres数据库转换为单个HTML表 - Data from MySQL and Postgres Databases into Single HTML Table with PHP 数据库中有两个不同的表,我想在一个表中按日期或时间的顺序从它们中获取数据? - there are two different tables in database i want to fetch data from them in one single table in a sequence through date or time? 如何在MySQL w / php中显示来自两个不同数据库的数据 - How to display data from two different databases in MySQL w/php PHP如何从表中获取所有数据并将其相应地放入div - PHP how to fetch all data from table and put it accordingly in a div 使用 PHP MySQL mysqli 从两个数据库中获取数据 - Fetch data from two databases using PHP MySQL mysqli 在PHP中从单个页面上的不同服务器中选择两个数据库 - select two databases from different server on single page in PHP 这是一种有效的方法,可以通过两个连接从两个不同的数据库获取数据 - Getting data from two different databases with single connection, is this a valid way 从具有不同列号的两个表中获取数据并根据日期对它们进行排序 - fetch data from two table with different column numbers and order them based on date 如何从具有不同外键名称的两个表中获取数据 - how to fetch data from the two table with different foreign key names 如何从蛋糕php中的两个表中获取数据 - how to fetch data from two table in cake php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM