Page 1 of 1
Multi DB connection, locally, won't connect to 2nd DB
Posted: Fri Apr 11, 2014 4:31 am
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??
Re: Multi DB connection, locally, won't connect to 2nd DB
Posted: Fri Apr 11, 2014 7:49 am
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.
Re: Multi DB connection, locally, won't connect to 2nd DB
Posted: Fri Apr 11, 2014 7:50 am
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.
Re: Multi DB connection, locally, won't connect to 2nd DB
Posted: Fri Apr 11, 2014 8:14 am
by simonmlewis
Not even locally?