1

Can I get data from three different servers on my php application? Actually I have my data on three different servers and I want to generate a report with having data from all three servers. Can you please suggest me a code for this?

 
function dbcon(ipaddress,servername,serverpassword,databasename)
{
    $con = mysql_connect(ipaddress,servername,serverpassword);
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }

    mysql_select_db(databasename) or die ("Cant find DB");

}
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
awais
  • 135
  • 1
  • 1
  • 6
  • If you have 3 differents DB servers, it's possible, the only thing is, most of the servers are only accessible from localhost, so you have to configure them, and instead of using 127.0.0.1 (or localhost) in your code, you use the ip adress of 1 of those servers ... – HamZa Nov 10 '12 at 10:44
  • 2
    Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the [red box](http://j.mp/Te9zIL)? Learn about prepared statements instead, and use MySQLi - [this article](http://j.mp/QEx8IB]) will help you decide which. – Geek Num 88 Nov 10 '12 at 10:45

2 Answers2

1

Certainly that is possible. I assume (though you are not very clear in this) that you are talking about three database servers? Then all you have to do is:

  • make sure the database servers are accessible via network (tcp)
  • create three database connections instead of only one

Since opening a connection to a database server returns a handle you have something you can use to address a specific connection. The syntax required for opening the connection is clearly explained in the manual pages. I suggest you read them and take a look at the provided examples...

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • Thanks but i found a solution through WDDX web service. :) – awais Dec 11 '12 at 15:22
  • Would you mind sharing why you are using such a complex solution on top of your stack instead of simply opening three separate connections to the server? – arkascha Dec 11 '12 at 22:59
0

First:

Welcome to Stack Overflow! Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

In order to connect to three different databases, you'll need 3 connection objects, and query each separately. Make sure you have configured the receiving ends to correctly accept connections.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308