Enjoy an ad free experience by logging in. Not a member yet?
Register .
03-05-2008, 12:07 AM
PM User |
#1
New Coder
Join Date: Jan 2004
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Using 2 Seperate Database Connections
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:
PHP Code:
$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:
PHP Code:
$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.
Last edited by Pee-H-Pee; 03-05-2008 at 12:44 AM ..
Reason: Fix Typo in Code
03-05-2008, 12:09 AM
PM User |
#2
Regular Coder
Join Date: Feb 2005
Posts: 660
Thanks: 5
Thanked 14 Times in 14 Posts
Wouldn't it be easier to export the data from one host/database and import it into the other?
03-05-2008, 12:13 AM
PM User |
#3
New Coder
Join Date: Jan 2004
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
No, I can't do that...
So does that mean it can't be done?
03-05-2008, 12:14 AM
PM User |
#4
Regular Coder
Join Date: Oct 2004
Posts: 104
Thanks: 7
Thanked 2 Times in 2 Posts
In the 2nd code snippet, I believe:
PHP Code:
$dbhost = "localhost" ;
Should be:
PHP Code:
$dbhost2 = "localhost" ;
03-05-2008, 12:20 AM
PM User |
#5
New Coder
Join Date: Jan 2004
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
oh wow, I didn't notice the typo...
I'll try it that way and see...
03-05-2008, 12:21 AM
PM User |
#6
Senior Coder
Join Date: Aug 2006
Location: Floating around somewhere...
Posts: 2,034
Thanks: 18
Thanked 42 Times in 42 Posts
hes correct i spotted that off the mark, this kind of check should be done before posting, hope that it works for you though
03-05-2008, 12:27 AM
PM User |
#7
New Coder
Join Date: Jan 2004
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
It still didn't work... only one database works but not the other...
03-05-2008, 12:32 AM
PM User |
#8
Super Moderator
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,602
Thanks: 2
Thanked 398 Times in 391 Posts
Quote:
Originally Posted by
Pee-H-Pee
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 )
Does the one that isn't working allow remote connections? I believe it's disabled by default for security reasons.
03-05-2008, 12:36 AM
PM User |
#9
New Coder
Join Date: Jan 2004
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
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.
03-05-2008, 12:38 AM
PM User |
#10
Regular Coder
Join Date: Oct 2004
Posts: 104
Thanks: 7
Thanked 2 Times in 2 Posts
Can you give us a bit more code, like queries? How are you checking which one is working etc?
03-05-2008, 12:40 AM
PM User |
#11
Senior Coder
Join Date: Aug 2006
Location: Floating around somewhere...
Posts: 2,034
Thanks: 18
Thanked 42 Times in 42 Posts
03-05-2008, 12:43 AM
PM User |
#12
Senior Coder
Join Date: Aug 2006
Location: Floating around somewhere...
Posts: 2,034
Thanks: 18
Thanked 42 Times in 42 Posts
Quote:
Originally Posted by
Pee-H-Pee
, 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.
03-05-2008, 12:50 AM
PM User |
#13
Super Moderator
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,602
Thanks: 2
Thanked 398 Times in 391 Posts
Quote:
Originally Posted by
Pee-H-Pee
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:
PHP Code:
$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.
03-05-2008, 01:03 AM
PM User |
#14
New Coder
Join Date: Jan 2004
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
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...
PHP Code:
$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:
PHP Code:
$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:
PHP Code:
// 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.
Last edited by Pee-H-Pee; 03-05-2008 at 02:12 AM ..
03-05-2008, 01:42 AM
PM User |
#15
Supreme Master coder!
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
You sure its not remote_table rather than romote_table?
__________________
|||| If you are getting paid to do a job, don't ask for help on it! ||||
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 04:10 AM .
Advertisement
Log in to turn off these ads.