how to connect multiple database servers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

how to connect multiple database servers

Post by itsmani1 »

Hi all....

my question is if we have more than one database server and with different type of access rights like on server one we can write and on server 2 and 3 we can't write, we have just reading access.

how to make the connection string for this type of Scenario?

Remember we all the databases are for one application.

any help?

thanks in advance.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

You can create multiple database connections.

Code: Select all

$database1 = mysql_connect('severname', $username, $password);
mysql_select_db('database_name', $database1);

$database2 = mysql_connect('servername2', $username2, $password2);
mysql_select_db('database_name2', $database2);
You can then query each specific database server by using the resource links you just created.

Code: Select all

//this will query the $database1 connection
$sample_query = mysql_query("SELECT * FROM `users`", $database1);

//this will query the $database2 connection
$sample_query2 = mysql_query("INSERT INTO  `users` VALUES('bob')", $database2);
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

thanks man

I will try this and hope this will work easily.
Post Reply