Forum Moderators: coopster

Message Too Old, No Replies

Keeping MySQL connection

         

ramoneguru

7:16 am on Apr 4, 2005 (gmt 0)

10+ Year Member



Is there a way to open a mysql connection and have it remain open while a user is at my site? Is it better to just keep a connection throughout their visit or connect only when the database is needed? Right now I have these two lines at the beginning of most pages (about 6 pages):

mysql_connect($host, $user, $pwd)
mysql_select_db($dbase)

I was thinking something involving sessions would work, but I am having trouble with it...
--Nick

balian

7:56 am on Apr 4, 2005 (gmt 0)

10+ Year Member



mysql_pconnect()
Used this one ahead of connect
mssql_pconnect() acts very much like mssql_connect() with two major differences.

First, when connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.

Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use (mssql_close() will not close links established by mssql_pconnect()).

This type of links is therefore called 'persistent'.

ironik

12:19 am on Apr 5, 2005 (gmt 0)

10+ Year Member



If you're asking if you have to call mysql_connect or mysql_pconnect on every page, you do.

A persistent connection is a connection that `remains alive`, but still requires the php function call to `pick up the connection`. On large traffic websites persistent connections can help reduce the amount of work the server has to do opening and closing connections, but it does take up extra resources (also be wary of using them if you are using transactions as open threads can remain until the connection is picked up again... big trouble if you have an uncommitted transaction).

A persistent connection will not mean that you don't have to call the database connection on each page.

If you want to save yourself the hassle of putting you database connection code at the top of each page then consider putting it in a seperate file and call it using the include() function:


<?php
include('dbconnect.php');
// Database enabled code here
?>

ramoneguru

3:49 am on Apr 5, 2005 (gmt 0)

10+ Year Member



Cool, thanks for the replies.
--Nick