Multi DB connection, locally, won't connect to 2nd DB

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Multi DB connection, locally, won't connect to 2nd DB

Post by simonmlewis »

Code: Select all

$dbuk = mysql_connect("localhost","root","")or die ('Could not connect to site1.co.uk');
$db2uk = mysql_connect("localhost","root","")or die ('Could not connect to site2.co.uk');
mysql_select_db('site1', $dbuk) or die ('Could not connect to site1.co.uk Database');
mysql_select_db('site2', $db2uk)or die ('Could not connect to site2.co.uk Database')or die(mysql_error());
We are trying to run tests between two entirely separately hosted web sites.
For some reason, when we run queries locally, it only checks on the first database, doesn't query the second one at all.
It's as though, the queries to check the first and second DB, are both done on the first one only.

Run in a LIVE environment, they work just fine (obviously tho with username and passwords and "localhost" being an IP address.

Is the problem just that I have to use usernames on both internal DBs??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Multi DB connection, locally, won't connect to 2nd DB

Post by Weirdan »

http://us2.php.net/mysql_connect
PHP Manual wrote: If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned.
There's a parameter to prevent that, called new_link.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Multi DB connection, locally, won't connect to 2nd DB

Post by Celauran »

The connection strings are identical, so $db2 is just a reference to $db. See Weirdan's solution above. As a matter of best practice, though, you should probably create separate DB users for each project, and you definitely shouldn't be connecting as root.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Multi DB connection, locally, won't connect to 2nd DB

Post by simonmlewis »

Not even locally?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply