Jump to content

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


Jessica

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.