Brian Dunning wrote:
In one case I do need to jump back and forth between databases that are on different physical servers. What's the most efficient way to handle this?


On Aug 2, 2005, at 11:14 AM, Devananda wrote:

Brian Dunning wrote:

My RaQ4 is throwing the "Too many connections" error. Now, it is getting hit pretty hard by traffic, but I wonder if my coding might be exacerbating this error. Due to my relatively lazy structure of includes and functions, my pages might have half a dozen or so of these (most are to the same database):
$connection = mysql_connect($db_host, $db_username, $db_password);
mysql_select_db($db_name, $connection);
Is that creating half a dozen connections, even though it's just one web user?


Yep. Each call to mysql_connect creates a new connection to the database. If you need to connect to multiple database servers then you obviously need multiple connections, but if you are only connecting to one database server, then you should really reuse a single connection. FYI, the process of creating a new connection requires substantially more processing time than executing a simple query (ie, mysql_select_db).

One common practice is to have a single include file which creates the database connection and instantiates a global variable with the handle to that connection. Then, all pages which include that file merely use that handle to communicate with the database.

Regards,
Devananda vdv




Create two connections at the beginning of your script/page, then query each as you need to. For example,

$connA = mysql_connect("A.server.com",...);
mysql_select_db("$db_name",$connA);

$connB = mysql_connect("B.server.com",...);
mysql_select_db("$another_db_name",$connB);

$hdl_some_query_on_A = mysql_query("SELECT something FROM sometable", $connA); # this query goes to A.server.com $hdl_some_query_on_B = mysql_query("SELECT some_other_thing FROM another_table",$connB); # this one goes to B.server.com


and so on. By storing the resource returned from mysql_connect, you can tell mysql_query which connection to use, and thus utilize multiple connections (but only create each connection once).


Regards,
Devananda vdv


--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to