Jump to content

[SOLVED] Cannot Connect to two databases at same time.


Recommended Posts

I have two databases. One has a table items, and another has a table news. (example.)
I want to connect to both in my script (in my config file, so at the beginning of every page.)

When I connect to the first, and query the items table, it works fine. I can print out the results, so I know it worked.
I then connect to the second, query the news table, and that works.

If I try to connect to both at the beginning, the first connection gets replaced by the second. I used to be able to do this with different users, but after I switched servers I now have to use the same user and password to access both.

Is that why this is not working? Any ideas how I can do this so I don't have to combine the databases? I cannot use a second user (I have already pleaded with (mt) and got a no.)
(In reality, there are 4 databases and they are all very big, so I don't want to combine them if at all possible.)

Sample Code:
[code]<?
$db_username = 'username';
$db_password = 'password';
$db_database = 'database_1';
$db_server = 'myhost';

$dbOrig = mysql_connect($db_server, $db_username, $db_password);
if(!$dbOrig){
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_database, $dbOrig);

$db_database = 'database_2';
$dbMain = mysql_connect($db_server, $db_username, $db_password);
if(!$dbMain){
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_database, $dbMain);


$sql = "SELECT * FROM items";
$rs = mysql_query($sql, $dbOrig);
if($rs){
while ($row = mysql_fetch_array($rs)) {
print_r($row);
}
}else{
die('Error: ' . mysql_error());
}

$sql = "SELECT * FROM news";
$rs = mysql_query($sql, $dbMain);
if($rs){
while ($row = mysql_fetch_array($rs)) {
print_r($row);
}
}else{
die('Error: ' . mysql_error());
}
?>[/code]

Output: Error: Table 'database_2.items' doesn't exist
[quote author=jesirose link=topic=121547.msg499917#msg499917 date=1168282262]
(In reality, there are 4 databases and they are all very big, so I don't want to combine them if at all possible.)
[/quote]

No, I don't want one database. It would have a hell of a lot of tables. Not just two.
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.