...

View Full Version : Using 2 Seperate Database Connections



Pee-H-Pee
03-05-2008, 01:07 AM
I am using a site that connects to a remote database to get all the info from. I am trying to also connect to my own database so I can combine information. Is that possible?

Here is my remote connection:


$dbhost = "remotesite.com";
$dbuser = "db_user";
$dbname = "db_name";
$dbpass = "password";
$db = mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname",$db);

When I try this:


$dbhost2 = "localhost";
$dbuser2 = "db_user";
$dbname2 = "db_name";
$dbpass2 = "password";
$db2 = mysql_connect("$dbhost2","$dbuser2","$dbpass2");
mysql_select_db("$dbname2",$db2);

Only one database is connected but not the other.

ptmuldoon
03-05-2008, 01:09 AM
Wouldn't it be easier to export the data from one host/database and import it into the other?

Pee-H-Pee
03-05-2008, 01:13 AM
No, I can't do that...
So does that mean it can't be done?

NeoPuma
03-05-2008, 01:14 AM
In the 2nd code snippet, I believe:

$dbhost = "localhost";
Should be:

$dbhost2 = "localhost";

Pee-H-Pee
03-05-2008, 01:20 AM
oh wow, I didn't notice the typo...
I'll try it that way and see...

rafiki
03-05-2008, 01:21 AM
hes correct i spotted that off the mark, this kind of check should be done before posting, hope that it works for you though

Pee-H-Pee
03-05-2008, 01:27 AM
It still didn't work... only one database works but not the other...

Inigoesdr
03-05-2008, 01:32 AM
It still didn't work... only one database works but not the other...

Which one isn't working?
How do you know it isn't working? Errors??
Add error checking. (See the manual (http://php.net/mysql_connect))
Does the one that isn't working allow remote connections? I believe it's disabled by default for security reasons.

Pee-H-Pee
03-05-2008, 01:36 AM
It seems whichever one I have on the bottom is the one that works... If I switch them it's still like that. So they are conflicting for some reason.

The one that doesn't work is checking for tables on the one that is working, which is a seperate db connection, and the error says the talble does not exist.

NeoPuma
03-05-2008, 01:38 AM
Can you give us a bit more code, like queries? How are you checking which one is working etc?

rafiki
03-05-2008, 01:40 AM
mysql_pconnect()? (php.net/mysql_pconnect)

rafiki
03-05-2008, 01:43 AM
, and the error says the talble does not exist.

Doesn't this say it all?
check table name, check table does exist and let us know.

Inigoesdr
03-05-2008, 01:50 AM
It seems whichever one I have on the bottom is the one that works... If I switch them it's still like that. So they are conflicting for some reason.

The one that doesn't work is checking for tables on the one that is working, which is a seperate db connection, and the error says the talble does not exist.

Wait, are you using the same username/password to access both databases? If so you need to add a boolean true to your connect call, after the password:

$db = mysql_connect($dbhost, $dbuser, $dbpass, true);
$db2 = mysql_connect($dbhost2, $dbuser2, $dbpass2, true);
By default PHP will reuse the first connection if you use the same login information.

Pee-H-Pee
03-05-2008, 02:03 AM
Ok, this is the db connection my site mainly runs off of.
The majority of the content is from a remote db and everything works perfectly fine the way it should be...



$dbhost = "remotesite.com";
$dbuser = "db_user";
$dbname = "db_name";
$dbpass = "password";
$db = mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname",$db);


Now I want to add content from my own database so I am using this:



$dbhost2 = "localhost";
$dbuser2 = "db_user";
$dbname2 = "db_name";
$dbpass2 = "password";
$db2 = mysql_connect("$dbhost2","$dbuser2","$dbpass2");
mysql_select_db("$dbname2",$db2);


This is what my config file looks like:



// Remote DB connection
$dbhost = "remotesite.com";
$dbuser = "db_user";
$dbname = "db_name";
$dbpass = "password";
$db = mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname",$db);

// My DB connection
$dbhost2 = "localhost";
$dbuser2 = "db_user";
$dbname2 = "db_name";
$dbpass2 = "password";
$db2 = mysql_connect("$dbhost2","$dbuser2","$dbpass2");
mysql_select_db("$dbname2",$db2);


Once I add the bottome part in the config file to add the connection to my own database it brings up the data but then the remote connection breaks...

Instead of bringing up the data for the remote connection it is checking for the table on my own database.

Table 'my_connection.remote_table' doesn't exist

Each DB connection has it's own different username and password.

_Aerospace_Eng_
03-05-2008, 02:42 AM
You sure its not remote_table rather than romote_table?

Pee-H-Pee
03-05-2008, 03:14 AM
it was just an example...

Table 'my_connection.remote_table' doesn't exist

my_connection = my connection to my own db
remote_table = the table on the remote connection that my db connection seems to be looking for...

Fumigator
03-05-2008, 05:00 PM
As has been suggested already, check each function to make sure it succeeded. You'll at least eliminate it as a problem.



$db = mysql_connect("$dbhost","$dbuser","$dbpass");
if (!$db) {
die("aaaaaahhhhhhhh the db connect failed.");
}

if (!mysql_select_db("$dbname",$db)) {
die ("ooooohhh nooooooooo the db select failed.");
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum