johnsmith153 Posted March 31, 2009 Share Posted March 31, 2009 This is what I need to do (connect to two databases at same time) $con1 = mysql_connect("server", "user", "pass"); if (!$con1) { echo "Could not connect to db 1: ". mysql_error(); exit; } //connect to db 2 $con2 = mysql_connect("server", "user", "pass"); if (!$con2) { echo "Could not connect to db 2: ". mysql_error(); exit; } //select db 1 mysql_select_db("dbase-name", $con1); The problem is the above will not work - it doesn't select the first connection again - as though it has been lost. However if I check if($con1) it DOES exist. If I tried to perform queries on connection 2 - this would work. Quote Link to comment https://forums.phpfreaks.com/topic/151937-simple-sql-connecting-to-2-databases-at-same-time/ Share on other sites More sharing options...
JonnoTheDev Posted March 31, 2009 Share Posted March 31, 2009 Are the databases on the same server. You dont have to open 2 connections if so, you can prepend the table name with the database name: mysql_query("SELECT * FROM db1.tablename ",$conn); mysql_query("SELECT * FROM db2.tablename ",$conn); Just dont use mysql_select_db() Quote Link to comment https://forums.phpfreaks.com/topic/151937-simple-sql-connecting-to-2-databases-at-same-time/#findComment-797888 Share on other sites More sharing options...
johnsmith153 Posted March 31, 2009 Author Share Posted March 31, 2009 They are not on the same server Quote Link to comment https://forums.phpfreaks.com/topic/151937-simple-sql-connecting-to-2-databases-at-same-time/#findComment-797889 Share on other sites More sharing options...
JonnoTheDev Posted March 31, 2009 Share Posted March 31, 2009 Try persistent connections $db1 = mysql_pconnect($host,$user,$pass); $db2 = mysql_pconnect($host,$user,$pass); mysql_select_db("db1",$db1); mysql_select_db("db2",$db2); Quote Link to comment https://forums.phpfreaks.com/topic/151937-simple-sql-connecting-to-2-databases-at-same-time/#findComment-797892 Share on other sites More sharing options...
johnsmith153 Posted March 31, 2009 Author Share Posted March 31, 2009 I prefer not to use persistent connections. is there another way? Quote Link to comment https://forums.phpfreaks.com/topic/151937-simple-sql-connecting-to-2-databases-at-same-time/#findComment-797897 Share on other sites More sharing options...
johnsmith153 Posted March 31, 2009 Author Share Posted March 31, 2009 Ensure every mysql function also has the exact resource specified. ie: $mysqlRes = mysql_query($sql,$con1); as: $mysqlRes = mysql_query($sql); may cause problems when using multiple connections Quote Link to comment https://forums.phpfreaks.com/topic/151937-simple-sql-connecting-to-2-databases-at-same-time/#findComment-797908 Share on other sites More sharing options...
lostnucleus Posted March 31, 2009 Share Posted March 31, 2009 by very its nature mysql_connect uses only one connection at a time to save resources , try to close the first connection b4 using another or use $conn1 = new mysqli($host,$username,$password,$databasename); $result = $conn->query('select.....'); $conn2 = new mysqli($host2,$username2,$password2,$databasename2); $result2 = $conn2->query('select.....'); for this make sure mysqli extension is loaded .... Quote Link to comment https://forums.phpfreaks.com/topic/151937-simple-sql-connecting-to-2-databases-at-same-time/#findComment-797920 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.