dhimok Posted July 11, 2007 Share Posted July 11, 2007 Hi everybody! Is it possible to connect to two mysql databases at the same time, and query both of them? Quote Link to comment Share on other sites More sharing options...
per1os Posted July 11, 2007 Share Posted July 11, 2007 Yep it is. Quote Link to comment Share on other sites More sharing options...
dhimok Posted July 11, 2007 Author Share Posted July 11, 2007 Ok. Here s an example how i connect to one of them $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = 'pass'; $dbname = 'database1'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname); // here s the website mysql_close($conn); And I want to connect to the other database then .... do I select the other database ... A quick example Quote Link to comment Share on other sites More sharing options...
per1os Posted July 11, 2007 Share Posted July 11, 2007 $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = 'pass'; $dbname = 'database1'; $dbname2 = 'database2'; $dbpass2 = 'dbpass2'; $dbuser2= 'dbuser2'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); $conn2 = mysql_connect($dbhost, $dbuser2, $dbpass2) or die ('Error connecting to mysql'); mysql_select_db($dbname, $conn); mysql_select_db($dbname2, $conn2); // here s the website mysql_query($query, $conn); mysql_query($query, $conn2); mysql_close($conn); Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 11, 2007 Share Posted July 11, 2007 Yes you can. You'll need to setup a link identifier when you connect to the mysql servers. Then when you use any mysql function that requires a link identifier (second parameter for function) you must provide it, Heres an example of how to use the link identifier: <?php // connect to mysql server 1 // $server1 variable is the link identifier for sending queries to server1 $server1 = mysql_connect('server1.mysite.com', 'server1username', 'server1password'); // connect to mysql server 2 // $server2 variable is the link identifier for sending queries to server2 $server2 = mysql_connect('server2.mysite.com', 'server2username', 'server2password'); // select database for server1: mysql_select_db('server1database', $server1); // select database for server2: mysql_select_db('server2database', $server2); // NOTE: We have used the link identifier above in the second parameter for mysql_select_db function. We do this for all mysql_* functions that accept link identifiers. here is a few more // query server1: $qry1 = mysql_query("SELECT * FROM table", $server1); // get results from query1: $rows = mysql_fetch_array($qry1); // query server2: $qry = mysql_query("SELECT * FROM table", $server2); // get results from query2: $rows = mysql_fetch_array($qry2); // NOTE: We don't provide a link identifier for mysql_fetch_* functions. They only require the resource identifier which passed down from a call to mysql_query. // close connection to servers: mysql_close($server1); mysql_close($server2); ?> EDIT: Frost beat me Quote Link to comment Share on other sites More sharing options...
dhimok Posted July 12, 2007 Author Share Posted July 12, 2007 Thank you for your quick answers Very helpful Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.