Categorized | MySQL

How do I connect to two MySQL databases at two different servers from a single php page?

Posted on 19 February 2010 by Abidoon

I need to extract data from 2 MySQL databases located at two different servers. How is it possible from one single PHP page?

Tags | , , , , , , ,

3 Responses to “How do I connect to two MySQL databases at two different servers from a single php page?”

  1. joe.attaboy says:

    This requires more detail than one can provide here, but all you really need to do is create two connections in the script.

    You create a connection the mysql_connect(), right? Well, you can do this for as many separate connections as you need, as long as you uniquely identify each one with a different variable name:

    $conn1 = mysql_connect(server, user, password)…
    $conn2 = mysql_connect(server2, user2, password2)…

    Now, you use the resource name (the variable name) to send database requests to each server.

    The script doesn’t care how many systems you connect to, as long as each one is uniquely identified.

  2. Colinc says:

    Simply use 2 connection variables. So for the first server :
    $conn1 = mysql_connect(“first.server.name”,”user”,:password);
    Second :
    $conn2 = mysql_connect(“second.server.name”,”user2″,”password2″);
    Then always make sure you include the connection in the query :
    $result = mysql_query(“select * from mytable”,$conn1);
    This is not a very efficient use of the database.

  3. unlockedhed says:

    As I understand it, the database you specify when connecting to mysql
    is your default database. For example, if you connect using “db1″,
    then to query that database, you can just use “SELECT * FROM table1″
    which, because of your default database, is equivalent to “SELECT *
    FROM db1.table1″. You are still able to access other databases by
    fully-qualifying the names, so even if “db1″ is your default database,
    you can still do something like “SELECT * FROM db2.table2″.

    Sean Colicchio, Server Engineer, http://HostMySite.com


Leave a Reply


 

February 2010
M T W T F S S
« Oct   Mar »
1234567
891011121314
15161718192021
22232425262728