bruckerrlb Posted August 4, 2007 Share Posted August 4, 2007 Okay, I know this has to be simple, but i've been searching it for a while now, I need to have two database connections open, database one will be to get information to query the second database. I am not sure how to set this up, I know how to get one database connected, but to get two databases on two different servers is a little above me and i'd love to know how to work it, any advice is much appreciated. I've been searching a while, and if you know of a website that has it, could you post it for me? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/63361-how-to-handle-2-mysql-database-connections-with-php/ Share on other sites More sharing options...
dbo Posted August 4, 2007 Share Posted August 4, 2007 at the top of your page or whatever you'll just define your connection credentials for both. $user1 = ""; $pass1= ""; $db1 = "" $host1 = ""; $connect1 = mysql_connect(....); $user2 = ""; ... .. .. $connect2 = mysql_connect(....); . . . mysql_disconnect($connect1); mysql_disconnect($connect2); Quote Link to comment https://forums.phpfreaks.com/topic/63361-how-to-handle-2-mysql-database-connections-with-php/#findComment-315771 Share on other sites More sharing options...
BlueSkyIS Posted August 4, 2007 Share Posted August 4, 2007 after connect2 = mysql_connect(), a mysql_query() is performed. Which connection does the mysql_query() act on? -BSIS Quote Link to comment https://forums.phpfreaks.com/topic/63361-how-to-handle-2-mysql-database-connections-with-php/#findComment-315774 Share on other sites More sharing options...
dbo Posted August 4, 2007 Share Posted August 4, 2007 ahh you'll want to return the link for each of them I guess and pass the link with the query. Quote Link to comment https://forums.phpfreaks.com/topic/63361-how-to-handle-2-mysql-database-connections-with-php/#findComment-315776 Share on other sites More sharing options...
BlueSkyIS Posted August 4, 2007 Share Posted August 4, 2007 ^^^ yes! I didn't know this and had been doing it a not-so-easy way myself. i think i found my own answer. mysql_query() takes an options resource link, so we can query each separately with: mysql_query($sql, $connect1); mysql_query($sql, $connect2); Quote Link to comment https://forums.phpfreaks.com/topic/63361-how-to-handle-2-mysql-database-connections-with-php/#findComment-315777 Share on other sites More sharing options...
dbo Posted August 4, 2007 Share Posted August 4, 2007 dbo rox? lol Quote Link to comment https://forums.phpfreaks.com/topic/63361-how-to-handle-2-mysql-database-connections-with-php/#findComment-315778 Share on other sites More sharing options...
bruckerrlb Posted August 5, 2007 Author Share Posted August 5, 2007 So you have to call mysql_query() everytime you want info from that query?? Quote Link to comment https://forums.phpfreaks.com/topic/63361-how-to-handle-2-mysql-database-connections-with-php/#findComment-316093 Share on other sites More sharing options...
BlueSkyIS Posted August 5, 2007 Share Posted August 5, 2007 no. use this to get the result of the query the first time and then use it as you like: $result = mysql_query($sql, $connect1); then you can do stuff like loop over results, etc: while ($row = mysql_fetch_array($result)) { // Do something with the row of data... } Quote Link to comment https://forums.phpfreaks.com/topic/63361-how-to-handle-2-mysql-database-connections-with-php/#findComment-316096 Share on other sites More sharing options...
bruckerrlb Posted August 5, 2007 Author Share Posted August 5, 2007 wonderful! So something like this would work $result = mysql_query($sql, $connect1); $result2 = mysql_query($sql, $connect2); $result3 = mysql_query($sql, $connect3); $result4 = mysql_query($sql, $connect4); while ($row = mysql_fetch_array($result) && $row1 = mysql_fetch_array($result2) && $row3 = mysql_fetch_array($result3) && $row4 = mysql_fetch_array($result4) ) { //can I use this to print out the results from the multiple queries, using $row4['example']; ? } Quote Link to comment https://forums.phpfreaks.com/topic/63361-how-to-handle-2-mysql-database-connections-with-php/#findComment-316097 Share on other sites More sharing options...
bruckerrlb Posted August 5, 2007 Author Share Posted August 5, 2007 also is && = "and" in php? Quote Link to comment https://forums.phpfreaks.com/topic/63361-how-to-handle-2-mysql-database-connections-with-php/#findComment-316098 Share on other sites More sharing options...
dbo Posted August 5, 2007 Share Posted August 5, 2007 I'm not sure what you're trying to accomplish with the chunk of code you wrote but yes logically that works fine. And yes && is and. Quote Link to comment https://forums.phpfreaks.com/topic/63361-how-to-handle-2-mysql-database-connections-with-php/#findComment-316108 Share on other sites More sharing options...
bruckerrlb Posted August 5, 2007 Author Share Posted August 5, 2007 sorry about my big chunk of code != making sense ...... what I was trying to accomplish in theory is to get all the results from all of the quieres and basically create on gigantic query to get all of the specific information I need ex. time between certain amount of hours and time betwee a certain amount of hours, all while equalling the client, something like that, please let me know if that still dosent' make sense Quote Link to comment https://forums.phpfreaks.com/topic/63361-how-to-handle-2-mysql-database-connections-with-php/#findComment-316113 Share on other sites More sharing options...
bruckerrlb Posted August 5, 2007 Author Share Posted August 5, 2007 This is what i'm trying to do in a nutshell, I honestly expect my computer to either laugh at me or melt when I try it, but this is what I ment when I was asking if it makes more sense //query the remote database for the specific user $query1 = "SELECT * FROM ei10800010001200708 WHERE NumeroExterno = '$productid'"; $queryfdesde = "SELECT * FROM ei10800010001200708 WHERE '$startdate' > FechaDesde"; $queryfhasta = "SELECT * FROM ei10800010001200708 WHERE '$enddate' < FechaHasta"; $queryhdesde = "SELECT * FROM ei10800010001200708 WHERE '$starthour' > HoraDesde"; $queryhhasta = "SELECT * FROM ei10800010001200708 WHERE '$endhour' < HoraFecha"; //make sure all the queries retured something if ($r1 = mysql_query ($query1) && $r2 = mysql_query($queryfdesde) && $r3 = mysql_query(queryfhasta) && $r4 = mysql_query($queryhdesde) && $r5 = mysql_query($queryhhasta)) { //putting all these values into a variable called $row1 while ($row1 = mysql_fetch_array($r1) && mysql_fetch_array($r2) && mysql_fetch_array($r3) && mysql_fetch_array($r4) && mysql_fetch_array($r5)) { //print everything print " <tr><td width=\"141\"><center>{$row1['NumeroExterno']}</center></td> <td><center>{$row1['NumeroExtension']}</center></td> <td><center>{$row['productname']}</center></td> <td><center>{$row1['FechaDesde']}</center></td> <td><center>{$row1['HoraDesde']}</center></td> <td><center>{$row1['Duracion']}</center></td> </tr>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/63361-how-to-handle-2-mysql-database-connections-with-php/#findComment-316117 Share on other sites More sharing options...
dbo Posted August 5, 2007 Share Posted August 5, 2007 The problem is if one of the queries runs out of data and the others still have data you're not going to be able to see the remaining data b/c the and causes it to fail. If they are related records why don't you run 1 query and join them all up? It's also my suggestion that you never select * and always select out the fields you want... even if it's every field. The reason being is that if you add fields in later... and not to the end of the table your code will break, which typically is unacceptable. Quote Link to comment https://forums.phpfreaks.com/topic/63361-how-to-handle-2-mysql-database-connections-with-php/#findComment-316123 Share on other sites More sharing options...
wildteen88 Posted August 5, 2007 Share Posted August 5, 2007 bruckerrlbPlease read your other post Quote Link to comment https://forums.phpfreaks.com/topic/63361-how-to-handle-2-mysql-database-connections-with-php/#findComment-316125 Share on other sites More sharing options...
bruckerrlb Posted August 5, 2007 Author Share Posted August 5, 2007 you are my hero!!! check your inbox for personal message Quote Link to comment https://forums.phpfreaks.com/topic/63361-how-to-handle-2-mysql-database-connections-with-php/#findComment-316133 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.