0

I've been trying to figure this out for a few days now. I have 4 databases that I need to connect to. I am trying to alternate between the connections but it wont let me. It always takes the last connection.

$link = mysql_connect("localhost","root",""); 
mysql_select_db("front",$link); 
$link2 = mysql_connect("mysite.com","user","2012");
mysql_select_db("db1",$link2);
$link3 = mysql_connect("mysite.com","user","2012");
mysql_select_db("appsdb",$link3);
$link4 = mysql_connect("mysite.com","user","2012", TRUE);
mysql_select_db("storagedb",$link4);

what do I need to do?

This works but takes too long

$link = mysql_connect("localhost","root",""); 
mysql_select_db("front",$link); 
$link2 = mysql_connect("mysite.com","user","2012",true);
mysql_select_db("db1",$link2);
$link3 = mysql_connect("mysite.com","user","2012",true);
mysql_select_db("appsdb",$link3);
$link4 = mysql_connect("mysite.com","user","2012", true);
mysql_select_db("storagedb",$link4);

Is it possible to separate the connections altogether but still use $link, $link2, $link3, $link4 inside the queries? I dont think its efficient to keep all connections open.

Dev-Ria
  • 329
  • 1
  • 7
  • 23
  • 1
    [Please, don't use mysql_* functions in new code.](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-function-in-php) They are no longer maintained and the [deprecation process](http://news.php.net/php.internals/53799) has begun on it. See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://uk3.php.net/pdo) or [MySQLi](http://uk1.php.net/mysqli) - this [article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – ajtrichards Dec 12 '12 at 17:34
  • 1
    I am aware of it. I can't make the changes until next month since I have too many queries to go back to. at this time I have no option but to use the older functions just till the end of the year. – Dev-Ria Dec 12 '12 at 17:36
  • Just wanted to make you aware :-) I've added an answer below. – ajtrichards Dec 12 '12 at 17:37

4 Answers4

3

I won't comment on using mysqli like others did, I guess from all these comments, you are already aware that you should switch, so I will just reply with the answer and maybe it can help other people who just can't upgrade php for some reason.

The function mysql_connect creates OR reuse a connection, so if you create multiple connections to the same server with the same credentials, you will get just a single connection.

If you NEED to force mysql_connect to create a new connection, you need to specify the new_link parameter as true as follow:

$link = mysql_connect("localhost","root","",true); 
mysql_select_db("front",$link); 
$link2 = mysql_connect("mysite.com","user","2012",true);
mysql_select_db("db1",$link2);
$link3 = mysql_connect("mysite.com","user","2012",true);
mysql_select_db("appsdb",$link3);
$link4 = mysql_connect("mysite.com","user","2012", true);
mysql_select_db("storagedb",$link4);

You can find more information here: http://php.net/manual/es/function.mysql-connect.php

Mickle Foretic
  • 1,399
  • 12
  • 23
  • my issue is that since the all connections are open the page takes too long to load. is there another way to do this without leaving them open? – Dev-Ria Dec 13 '12 at 13:37
  • 1
    You could mysql_close() them if you won't use them anymore. Your other option would be modifying your queries so they won't need a different mysql_select_db, and insted using the full table names on the query. Example: Instead of using mysql_select_db("appsdb") and then "select * from table", you would do "select * from appsdb.table". That way you could convert all the connections to the same db into a single one. – Mickle Foretic Dec 13 '12 at 14:11
0

Questions like these are rampant in this site, I suppose your search for one like this for more help.

$db_conn = connect_db(host, user, pwd);
mysql_select_db('existing_db', $db_conn);
 -- do selects and scrub data --
mysql_select_db('new_db', $db_conn);
-- insert the required data --

Full link Here

Community
  • 1
  • 1
  • I dont want to have to switch databases before every query. i want to just use $link, $link2, $link3 to specify which db to connect to in my query – Dev-Ria Dec 12 '12 at 17:55
  • Have you tried using arrays? make array for databases, hosts, users and just use the arrays inside the mysql_connect(); function –  Dec 12 '12 at 18:02
  • can you give me an example of how that would look? its a pretty good idea – Dev-Ria Dec 13 '12 at 13:36
  • not useful answer, except if you edit it with more details ;) –  Dec 22 '12 at 13:53
0

just add the ",$link" to the end of your query statement...

mysql_query("SELECT * FROM table", $link); 
Rich701
  • 311
  • 1
  • 4
  • 10
  • I'm doing this but it only connects to last db. other pages give me Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given – Dev-Ria Dec 12 '12 at 17:56
  • $link = mysql_connect("localhost","root","");mysql_select_db("front",$link); $link2 = mysql_connect("mysite.com","user","2012", true); mysql_select_db("db1",$link2); $link3 = mysql_connect("mysite.com","user","2012", true); mysql_select_db("appsdb",$link3); $link4 = mysql_connect("mysite.com","user","2012", true); mysql_select_db("storagedb",$link4); – Rich701 Dec 12 '12 at 18:00
  • this worked but it seems inefficient cause it takes a long time to load pages. Is there a way to completely keep the connections separate and only open them when I use them but still use just $link2/3/4 – Dev-Ria Dec 12 '12 at 18:30
0

why not using persistent connections mysql_pconnect, but consider to switch to pdo or mysqlnd
(counterparts of persistent connections 1 2)

Community
  • 1
  • 1