Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-05-2008, 12:07 AM   PM User | #1
Pee-H-Pee
New Coder

 
Join Date: Jan 2004
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Pee-H-Pee is an unknown quantity at this point
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
Pee-H-Pee is offline   Reply With Quote
Old 03-05-2008, 12:09 AM   PM User | #2
ptmuldoon
Regular Coder

 
Join Date: Feb 2005
Posts: 660
Thanks: 5
Thanked 14 Times in 14 Posts
ptmuldoon is on a distinguished road
Wouldn't it be easier to export the data from one host/database and import it into the other?
ptmuldoon is offline   Reply With Quote
Old 03-05-2008, 12:13 AM   PM User | #3
Pee-H-Pee
New Coder

 
Join Date: Jan 2004
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Pee-H-Pee is an unknown quantity at this point
No, I can't do that...
So does that mean it can't be done?
Pee-H-Pee is offline   Reply With Quote
Old 03-05-2008, 12:14 AM   PM User | #4
NeoPuma
Regular Coder

 
Join Date: Oct 2004
Posts: 104
Thanks: 7
Thanked 2 Times in 2 Posts
NeoPuma is an unknown quantity at this point
In the 2nd code snippet, I believe:
PHP Code:
$dbhost "localhost"
Should be:
PHP Code:
$dbhost2 "localhost"
NeoPuma is offline   Reply With Quote
Old 03-05-2008, 12:20 AM   PM User | #5
Pee-H-Pee
New Coder

 
Join Date: Jan 2004
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Pee-H-Pee is an unknown quantity at this point
oh wow, I didn't notice the typo...
I'll try it that way and see...
Pee-H-Pee is offline   Reply With Quote
Old 03-05-2008, 12:21 AM   PM User | #6
rafiki
Senior Coder

 
rafiki's Avatar
 
Join Date: Aug 2006
Location: Floating around somewhere...
Posts: 2,034
Thanks: 18
Thanked 42 Times in 42 Posts
rafiki will become famous soon enough
hes correct i spotted that off the mark, this kind of check should be done before posting, hope that it works for you though
__________________
Get Firefox Now
rafiki is offline   Reply With Quote
Old 03-05-2008, 12:27 AM   PM User | #7
Pee-H-Pee
New Coder

 
Join Date: Jan 2004
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Pee-H-Pee is an unknown quantity at this point
It still didn't work... only one database works but not the other...
Pee-H-Pee is offline   Reply With Quote
Old 03-05-2008, 12:32 AM   PM User | #8
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,602
Thanks: 2
Thanked 398 Times in 391 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Quote:
Originally Posted by Pee-H-Pee View Post
It still didn't work... only one database works but not the other...
  1. Which one isn't working?
  2. How do you know it isn't working? Errors??
  3. Add error checking. (See the manual)
  4. Does the one that isn't working allow remote connections? I believe it's disabled by default for security reasons.
Inigoesdr is offline   Reply With Quote
Old 03-05-2008, 12:36 AM   PM User | #9
Pee-H-Pee
New Coder

 
Join Date: Jan 2004
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Pee-H-Pee is an unknown quantity at this point
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.
Pee-H-Pee is offline   Reply With Quote
Old 03-05-2008, 12:38 AM   PM User | #10
NeoPuma
Regular Coder

 
Join Date: Oct 2004
Posts: 104
Thanks: 7
Thanked 2 Times in 2 Posts
NeoPuma is an unknown quantity at this point
Can you give us a bit more code, like queries? How are you checking which one is working etc?
NeoPuma is offline   Reply With Quote
Old 03-05-2008, 12:40 AM   PM User | #11
rafiki
Senior Coder

 
rafiki's Avatar
 
Join Date: Aug 2006
Location: Floating around somewhere...
Posts: 2,034
Thanks: 18
Thanked 42 Times in 42 Posts
rafiki will become famous soon enough
mysql_pconnect()?
__________________
Get Firefox Now
rafiki is offline   Reply With Quote
Old 03-05-2008, 12:43 AM   PM User | #12
rafiki
Senior Coder

 
rafiki's Avatar
 
Join Date: Aug 2006
Location: Floating around somewhere...
Posts: 2,034
Thanks: 18
Thanked 42 Times in 42 Posts
rafiki will become famous soon enough
Quote:
Originally Posted by Pee-H-Pee View Post
, 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.
__________________
Get Firefox Now
rafiki is offline   Reply With Quote
Old 03-05-2008, 12:50 AM   PM User | #13
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,602
Thanks: 2
Thanked 398 Times in 391 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Quote:
Originally Posted by Pee-H-Pee View Post
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$dbpasstrue);
$db2 mysql_connect($dbhost2$dbuser2$dbpass2true); 
By default PHP will reuse the first connection if you use the same login information.
Inigoesdr is offline   Reply With Quote
Old 03-05-2008, 01:03 AM   PM User | #14
Pee-H-Pee
New Coder

 
Join Date: Jan 2004
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Pee-H-Pee is an unknown quantity at this point
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..
Pee-H-Pee is offline   Reply With Quote
Old 03-05-2008, 01:42 AM   PM User | #15
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
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!||||
_Aerospace_Eng_ is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:10 AM.


Advertisement
Log in to turn off these ads.