Jump to content

how to handle 2 mysql database connections with php


bruckerrlb

Recommended Posts

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

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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...

 

 

}

 

Link to comment
Share on other sites

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']; ?

 

 

}

Link to comment
Share on other sites

sorry about my big chunk of code != making sense  :D......

 

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

Link to comment
Share on other sites

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>";

 

 

 

}

 

}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.