Jump to content

Connecting to two Mysql databases (one remote)


xwishmasterx

Recommended Posts

Hello

 

I have followed a few guides / ideas but I simple can't get this too work properly. I can connect to the remote server and do quries and all, but getting anything from my own server (where the script is being runned) is just not happening. Does anyone have a link or example I can look at?

Link to comment
Share on other sites

If you want help with what your code is doing, you will need to post your code, along with any errors or symptoms you saw that lead to you believe your script is not working.

 

If you have two active database connections in one script, it is likely that the problem is that you need to specify the correct connection when you execute a query against the database where your script is running at.

Link to comment
Share on other sites

What I have done is this, following an example I found..

 

<?php
$server = "xxxxxxxxxxxxxxx";
$dbname = "lxxxxxxxxxxxxxxx";
$user = "xxxxxxxxxx"; //Allowed for the ip
$password = "xxxxxxxxxxxx";
$link = mysql_connect($server,$user,$password) or die(mysql_error());
mysql_select_db($dbname);  
?>
<?php 
$hostname = "localhost";
$username = "xxxxxxxxxx";
$password = "xxxxxxxxxxxxxxxx";
$dbname2 = "xxxxxxxxxx";
$dbh1 = mysql_connect($hostname, $username, $password) or die(mysql_error());
mysql_select_db($dbname2);  
?>

 

I have tried these to queries:

<?php
$home_members = mysql_query ("SELECT DISTINCT (credit_members_id) as homememberid FROM vtp_tracking WHERE action_date>date_sub(NOW(),interval 5 minute) AND credit_members_id=57983", $link)
or die(mysql_error()); 
$rs_home_members = mysql_fetch_array( $home_members );
$home_member = $rs_home_members['homememberid'];
echo " $home_member ";
?>

<?php
$away_members = mysql_query ("SELECT name FROM vtp_members WHERE id='58106' ", $dbh1)
or die(mysql_error()); 
$rs_away_members = mysql_fetch_array( $away_members );
$away_member = $rs_away_members['name'];
echo " $away_member ";

?>

 

I can get a result from the first query, but none from the other. I am certain that it is the connection that is the problem or the wrong database.

Link to comment
Share on other sites

Again, you didn't state what symptom or error you are getting that leads you to believe the second one is not working.

 

BTW - mysql_error() would need the correct link resource so that it would output the last error that occurred on the specific connection. You also need to use the correct link resource in the mysql_select_db statements.

Link to comment
Share on other sites

I have checked the connections and both are fine. It seems the problem running this part, when the other connection is there:

 

<?php
$away_members = mysql_query ("SELECT name FROM vtp_members WHERE id='58106' ", $dbh1)
or die(mysql_error()); 
$rs_away_members = mysql_fetch_array( $away_members );
$away_member = $rs_away_members['name'];
echo " $away_member ";

?>

 

If I remove the connection to the remote server the above part works fine.

Link to comment
Share on other sites

There's at least two different things wrong with the code, that someone already posted -

BTW - mysql_error() would need the correct link resource so that it would output the last error that occurred on the specific connection. You also need to use the correct link resource in the mysql_select_db statements.

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.