I think that generally PHP closes any non-persistent connections for you
when you leave your script, so that it's not necessary to clean up after
your script is done.

You can create persistent connections, but in a high-traffic site they
tend to become a resource problem after a while--and your server will be
able to maintain only a finite number of connections open anyway. 

IMHO, before worrying about keeping the connections open, it's a good
idea to take a good look at your setup and determine whether there are
ways to improve upon its design. For example, you can use replication
and have an instance of MySQL running on each web server, so that all
database connections are local and performed through UNIX sockets, which
are generally the fastest (assuming you're running UNIX, of course).
This may require a bit of data redesign, but it's worked for me in the

php|architect - The magazine for PHP Professionals
The monthly worldwide magazine dedicated to PHP programmers

Come visit us at http://www.phparch.com!
--- Begin Message ---
I have a general question concerning databases in php.  More specifically,
what is the general consensus on maintaining a connection.  Some of this is
more than likely my ignorance.

My connection stuff goes something like this ...

$db = mysql_connect("localhost", "stuff", "pw") or die("Could Not Connect");
mysql_select_db("stuff",$db) or die("Could Not Select Database");

<<< Other Code Here >>>


It seems very inefficient to open and close a database with each different
page or call to that database.  My question is ... Is there a better way
and, if so, what is it?  I guess I do not quite understand just how long a
connection is maintained.  I hate to write code that doesn't clean up after
itself.  Any suggestions are appreciate.  TIA


PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

--- End Message ---
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to