Reputation: 41
I have to put the data into a php page from the databases which are in seperate hosts. 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? I am using 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.
Upvotes: 1
Views: 1246
Reputation: 2852
First of all you have not mentioned the Database name which you are using. I assume that it will be 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. 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..
Like conn1 for DB1 and conn2 for DB2.
Also you ca use schema name if your DB allows.
Upvotes: 0
Reputation: 365
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.
Upvotes: 0
Reputation: 17725
1) Connect to first database
2) Fetch and save the data in an array
3) Connect to second database
4) Fetch and save the data in the same array
5) Loop through the array and display results
Upvotes: 1