klb Posted June 18, 2007 Share Posted June 18, 2007 is it possible to have two open connections to two different mysql dbs at the same time? when i tried it, only the one on the bottom of the list was active. my config file looks like this: //---------------------------------------------// $dbname = 'xxx'; # Database Name $dbuser = 'xxx'; # Database Username $dbpass = 'xxx'; # Database Password $dbhost = 'xxx'; # Database Host $conn2 = mysql_connect($dbhost,$dbuser,$dbpass) or die ("Could not connect to $dbname: ".mysql_error()); mysql_select_db($dbname) or die ("Could not access the database: ".mysql_error()); $dbname5 = 'yyy'; # Database Name $dbuser5 = 'yyy'; # Database Username $dbpass5 = 'yyy'; # Database Password $dbhost5 = 'yyy'; # Database Host $conn = mysql_connect($dbhost5,$dbuser5,$dbpass5) or die ("Could not connect to $dbname5: ".mysql_error()); mysql_select_db($dbname5) or die ("Could not access the database: ".mysql_error()); //--------------------------------------// so i want to be able to do mysql_query($query,$conn2) when i need to access xxx db, but it doesn't seem to work that way. am i doing something incorrectly? any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/56071-connecting-to-two-mysql-databases-simultaneously/ Share on other sites More sharing options...
akitchin Posted June 18, 2007 Share Posted June 18, 2007 although it appears you're making sure to adjust the parameters, the new_link parameter in mysql_connect() might help: http://ca3.php.net/manual/en/function.mysql-connect.php Quote Link to comment https://forums.phpfreaks.com/topic/56071-connecting-to-two-mysql-databases-simultaneously/#findComment-276931 Share on other sites More sharing options...
klb Posted June 18, 2007 Author Share Posted June 18, 2007 thanks so much, that solved my problem. onto the rest of the problems now! Quote Link to comment https://forums.phpfreaks.com/topic/56071-connecting-to-two-mysql-databases-simultaneously/#findComment-277201 Share on other sites More sharing options...
teng84 Posted June 18, 2007 Share Posted June 18, 2007 if you connect at the first db then the second connection will overwrite the first one thats why the second db is connected so you can just add the connection to each query per db Quote Link to comment https://forums.phpfreaks.com/topic/56071-connecting-to-two-mysql-databases-simultaneously/#findComment-277203 Share on other sites More sharing options...
vikaspa Posted March 30, 2011 Share Posted March 30, 2011 to resolve this akitchin had suggested athis link http://ca3.php.net/manual/en/function.mysql-connect.php Just to under stand more can I we give $link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password'); A connection to 1st database for example.com $link = mysql_connect('example2.com:3307', 'mysql_user', 'mysql_password'); A connection to 2nd database for example2.com Is this OK ? Also want to know how the port number is given who allocates this ? Does this mean these websites are from same name servers ? Actual code :suggested by akitchin Link : http://ca3.php.net/manual/en/function.mysql-connect.php Code <?php // we connect to example.com and port 3307 $link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($link); // we connect to localhost at port 3307 $link = mysql_connect('example2.com:3307', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($link); ?> Quote Link to comment https://forums.phpfreaks.com/topic/56071-connecting-to-two-mysql-databases-simultaneously/#findComment-1194291 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.