1

I've a website connected with database. When I fill the form the php script send the data to the database. The website and the database are on same server. To store the data in database I use:

    $con=mysql_connect('localhost',$id,$password);
    mysql_select_db($dabase,$con);
    $query=mysql_query($queryToRun);

This is how I send data to the database. Now I want to store the same form on a database stored on another server. Is it possible, if yes then how? Any kind of help is appreciated very much.

Sikander
  • 447
  • 4
  • 26

3 Answers3

2

Instead of "localhost", enter in the ip address / host name of the server where database server is listening for requests. In order for this to work, that database server will have to allow requests from outside "localhost" aswell

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
1

The remote database needs to be configured to allow remote connections. See this question.

Once that is set up, you just need to set the server IP/hostname in the mysql_connect() call instead of localhost.

Side note: The mysql_* API is deprecated, it is recommended to upgrade to MySQLi or PDO.

Community
  • 1
  • 1
MrCode
  • 63,975
  • 10
  • 90
  • 112
1

You could just create two concurrent sets of connection variables and send the query to both of the database servers under question. For example, you could have:

  $dbConn1 = mysql_connect('address-of-the-first-database-server', 
  $userName1, $password1);
  mysql_select_db($dbName1, $dbConn1);

  $dbConn2 = mysql_connect('address-of-the-second-database-server', 
  $userName2, $password2);
  mysql_select_db($dbName2, $dbConn2);

  $execQuery = mysql_query($queryToRun, $dbConn1);
  $execQuery = mysql_query($queryToRun, $dbConn2);

PS: I would also recommend using more descriptive names for your variables, as this basically makes programming feel like writing structured English.

Priidu Neemre
  • 2,813
  • 2
  • 39
  • 40
  • Yes, it should make the query run on two distinct servers (or in two different databases of the same server, in case you choose to use the same server address but different database names). NOTE: You should take my syntax with a grain of salt as I do not write PHP too often. I hope I got it right though. – Priidu Neemre Dec 18 '12 at 08:32