Click here to Skip to main content
15,907,910 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone

i want to know about how can i connect two database that are located on two different database server in php

thanks
Posted

1 solution

A) You want to access two databases from the same php code?
There is nothing special about. Whatever mysql interface you choose in php (old one, MySQLi, PDO_MySQL), you get a context resource or an object you can (have to) use in all your future activities related to that database connection.

or

B) You want to have sql query involving two different databases?
Well, that does not involve php directly, but the database engine. Look around here:
http://dev.mysql.com/doc/refman/5.1/en/federated-storage-engine.html[^], http://winashwin.wordpress.com/2012/08/22/mysql-federated-table/[^]
 
Share this answer
 
Comments
ambujmalviya 13-Oct-12 3:05am    
i want to connect both the databases and insert value in both database from a single php page
Zoltán Zörgő 13-Oct-12 6:43am    
Than use the first approach. If you can insert in any of them, you can insert in both? What is the problem? What have you tried?
ambujmalviya 13-Oct-12 7:09am    
my value are properly inserted in first database where the page is located but i did not understand how it can be inserted on other database that is situated on different server.
Zoltán Zörgő 13-Oct-12 9:09am    
Well, connect to the other server too, and insert. Do you have access to that mysql server? Do you have problems connecting to it ONLY from php?
Zoltán Zörgő 13-Oct-12 9:37am    
If you want to access the other server over the internet (you are not in the same network, or there are firewalls between the two nodes), you have to ensure that you can establish TCP level mysql connection. But the database servers are rarely exposed to the internet, due to security reasons. The ability to access a phpmyadmin on the other server does not mean, that you can access the database server directly. If this is the case, you have several options:
1) open the necessary ports, if you have the authority (not a good idea)
2) establish a VPN connection, if you have the authority (quite resource expensive)
3) establish an ssl tunnel to the other server
4) if none of the above are available, you have to expose some sort of service on the other server - supposing that it has a web server on, and you can create any cgi app there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900