how to connect more then one DB in php... but the DB servers are same . but the DB is different. same single page i need to fetch the result from all the 3 db to display. Thank you
+1
A:
Easy: make multiple connections. Each connection returns a resource handle which you assign to a variable. So you just put each connection into it's own variable.
staticsan
2010-03-25 05:53:27
A:
http://php.net/mysql_connect, note the parameters
also, if all these DBs share same server, you can just specify particular db using . syntax:
SELECT * FROM db1.table ...
SELECT * FROM db2.table ...
SELECT * FROM db3.table ...
Col. Shrapnel
2010-03-25 05:53:34
+1
A:
Method 1:
Don't select databases; put the database name before the table:
mysql_connect('localhost','db_user','pssword');
mysql_query('SELECT * FROM database_1.table_name');
Method 2:
$handle_db1 = mysql_connect("localhost","myuser","apasswd");
$handle_db2 = mysql_connect("127.0.0.1","myuser","apasswd");
$handle_db3 = mysql_connect("localhost:3306","myuser","apasswd");
$handle_db4 = mysql_connect("localhost","otheruser","apasswd");
mysql_select_db("db1",$handle_db1);
mysql_select_db("db2",$handle_db2);
mysql_select_db("db3",$handle_db3);
mysql_select_db("db4",$handle_db4);
//do a query from db1:
$query = "select * from test"; $which = $handle_db1;
mysql_query($query,$which);
//do a query from db2 :
$query = "select * from test"; $which = $handle_db2;
mysql_query($query,$which);
Coronatus
2010-03-25 05:54:59
You should note that method 1 only works if that user has privileges for those databases.
animuson
2010-03-25 05:57:38