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? Link to comment https://forums.phpfreaks.com/topic/59490-solved-a-quick-questionanswer/ Share on other sites More sharing options...
per1os Posted July 11, 2007 Share Posted July 11, 2007 Yep it is. Link to comment https://forums.phpfreaks.com/topic/59490-solved-a-quick-questionanswer/#findComment-295627 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 Link to comment https://forums.phpfreaks.com/topic/59490-solved-a-quick-questionanswer/#findComment-295636 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); Link to comment https://forums.phpfreaks.com/topic/59490-solved-a-quick-questionanswer/#findComment-295641 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 Link to comment https://forums.phpfreaks.com/topic/59490-solved-a-quick-questionanswer/#findComment-295645 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 Link to comment https://forums.phpfreaks.com/topic/59490-solved-a-quick-questionanswer/#findComment-296337 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.