Forum Moderators: coopster

Message Too Old, No Replies

php mysql databases and multiple servers

         

GoodMoJo

12:49 am on Dec 6, 2003 (gmt 0)

10+ Year Member



Is it possible to access a database via php from another server?
In other words I have a php database on a server. I want to access this database on a site that is not on the same server. Can I do this?

The code I generally use for connecting to a database looks like this.

$db = mysql_connect('localhost', 'user, 'password') or die("Couldn't connect to the database.");
mysql_select_db('database') or die("Couldn't select the database");

How would I change this to connect to a database on a differnt server? would i Just replace the 'localhost' with the site url? Are there any catches that I need to know about to make this work?

Thanks in advance.
-Chris

NickCoons

4:09 am on Dec 6, 2003 (gmt 0)

10+ Year Member



<would i Just replace the 'localhost' with the site url?>

Kinda.. you'd replace it with the hostname or IP address of the server you are accessing.

<Are there any catches that I need to know about to make this work?>

Yes.. you need to make sure that you've configured the user you're connecting with to be able to connect to the server remotely instead of from localhost. You can check the MySQL documentation for details on how to configure the user in this way.

GoodMoJo

7:56 am on Dec 6, 2003 (gmt 0)

10+ Year Member



Many thanks