SCook Posted December 29, 2007 Share Posted December 29, 2007 Hi all, Is there a good way to be able to open a connection to more than one db at a time? I'm doing something that will need it's own db, but I also need a connection to the first one which contains member info, etc. Any suggestions would be great, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/83642-accessing-multiple-databases-at-the-same-time/ Share on other sites More sharing options...
redarrow Posted December 29, 2007 Share Posted December 29, 2007 <?php include('adodb.inc.php'); # load code common to ADOdb $conn1 = &ADONewConnection('mysql'); # create a mysql connection $conn2 = &ADONewConnection('oracle'); # create a oracle connection $conn1->PConnect($server, $userid, $password, $database); $conn2->PConnect(false, $ora_userid, $ora_pwd, $oraname); $conn1->Execute('insert ...'); $conn2->Execute('update ...'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/83642-accessing-multiple-databases-at-the-same-time/#findComment-425497 Share on other sites More sharing options...
SCook Posted December 30, 2007 Author Share Posted December 30, 2007 I assume adodb.inc.php is a standard include? I'm not familiar with it. What I use to connect is the following: access = mysql_connect(host, user, pw);] mysql_select_db(db, access); So along that vane, is there a way to have two connections at once? Quote Link to comment https://forums.phpfreaks.com/topic/83642-accessing-multiple-databases-at-the-same-time/#findComment-425975 Share on other sites More sharing options...
papaface Posted December 30, 2007 Share Posted December 30, 2007 $con1 = mysql_connect("localhost","username","pw") or die (mysql_error()); $con2 = mysql_connect("localhost","username","pw") or die (mysql_error()); mysql_select_db(db, $con1) or die (mysql_error());//will run on connection 1 mysql_select_db(db, $con2) or die (mysql_error());//will run on connection 2 mysql_query("SOME SQL",$con1) or die (mysql_error());//will run on connection 1 mysql_query("SOME SQL",$con2) or die (mysql_error());//will run on connection 2 Quote Link to comment https://forums.phpfreaks.com/topic/83642-accessing-multiple-databases-at-the-same-time/#findComment-425979 Share on other sites More sharing options...
Barand Posted December 30, 2007 Share Posted December 30, 2007 If the 2 databases are on the same server then you only need a single connection to that server, assuming you have access privileges to both using the same username To access a second database on the server you only need to prepend the tablename with the database name <?php $con = mysql_connect($host, $user, $pwd); mysql_select_db (main_db); $sql1 = "SELECT * FROM mytable"; // from main db $sql2 = "SELECT m.id, m.name FROM other_db.members m"; // from other db $sql3 = "SELECT m.name, a.col1 FROM mytable a INNER JOIN other_db.members m ON a.member = m.id"; // from both Quote Link to comment https://forums.phpfreaks.com/topic/83642-accessing-multiple-databases-at-the-same-time/#findComment-426078 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.