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.

1
  • You will need to replace the 'localhost' with the servers FQDN or IP Address Commented Dec 18, 2012 at 8:16

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.