By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,300 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,300 IT Pros & Developers. It's quick & easy.

Sessions and persistent connections to MySQL database

P: n/a
I'm relatively new to PHP/MySQL and I've heard about maintaining a
persistent connection to the database. To save overhead, I'd like to
maintain the connection in a session variable, but I have a few
questions first.

I'm able to establish session variables and test them with isset() to
avoid re-defining them with each new web-page load. So is this really
as simple as

if (!isset($_SESSION['connection'])) {
$connection = mysql_connect("SomeServer","UserName","password");
$_SESSION['connection'] = $connection;
};

Will this work? and is there a better way to do it--or a more
conventional way? What should I be thinking about vis a vis security
regarding this connection.

Is there a recommended way to make the session's connection variable
"expire"? I suppose I could store the time the connection was
established and then check the elapsed time with each web-page
re-load, dumping the session if it's been too long.

Is there a preferred way to store $connection without using
$_SESSION[]?

Thanks,
M. Katz
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
M. Katz wrote:
I'm relatively new to PHP/MySQL and I've heard about maintaining a
persistent connection to the database. To save overhead, I'd like to
maintain the connection in a session variable, but I have a few
questions first.

I'm able to establish session variables and test them with isset() to
avoid re-defining them with each new web-page load. So is this really
as simple as

if (!isset($_SESSION['connection'])) {
$connection = mysql_connect("SomeServer","UserName","password");
$_SESSION['connection'] = $connection;
};

Will this work? and is there a better way to do it--or a more
conventional way? What should I be thinking about vis a vis security
regarding this connection.
I'm not sure that this will work. A so created MySQL connection will be
closed when the script ends. You will store the handle to connection in
the session variable but it will invalid since the real connection will
be closed between to script calls.

There is another function in the MySQL module for opening persistent
connections: mysql_pconnect.
Is there a recommended way to make the session's connection variable
"expire"? I suppose I could store the time the connection was
established and then check the elapsed time with each web-page
re-load, dumping the session if it's been too long.
Using mysql_pconnect, the connection life-time will be managed by PHP.
You don't need to make any explicite connection closure and after a
while, remaining connections will be automatically closed by PHP.
Is there a preferred way to store $connection without using
$_SESSION[]?

Thanks,
M. Katz


Guillaume Brocker
Jul 17 '05 #2

P: n/a
M. Katz wrote:
I'm relatively new to PHP/MySQL and I've heard about maintaining a
persistent connection to the database. To save overhead, I'd like to
maintain the connection in a session variable, but I have a few
questions first.


If I am not entirely correct, somebody correct me....
MySQL connection is a resource variable and thus cannot
be used and stored into a session. It will not work.
I tried it, before I found out this, and it did not. Maybe I
did something wrong....

For what you want to accomplish use mysql_pconnect which gives
you a reusable persistent connection to mysql.

Tomy.
Jul 17 '05 #3

P: n/a
On 10 Feb 2004 00:39:53 -0800, M. Katz wrote:
I'm relatively new to PHP/MySQL and I've heard about maintaining a
persistent connection to the database. To save overhead, I'd like to
maintain the connection in a session variable, but I have a few
questions first.

I'm able to establish session variables and test them with isset() to
avoid re-defining them with each new web-page load. So is this really
as simple as

if (!isset($_SESSION['connection'])) {
$connection = mysql_connect("SomeServer","UserName","password"); you must use mysql_pconnect instead
$_SESSION['connection'] = $connection;
};

Will this work? and is there a better way to do it--or a more
conventional way? What should I be thinking about vis a vis security
regarding this connection.

Is there a recommended way to make the session's connection variable
"expire"? I suppose I could store the time the connection was
established and then check the elapsed time with each web-page
re-load, dumping the session if it's been too long.

Is there a preferred way to store $connection without using
$_SESSION[]?

i think that you dont have to store it in session variables
just use permanent connection (mysql_pconnect).
It's all explained in PHP manual

Jul 17 '05 #4

P: n/a
Hi M!

On 10 Feb 2004 00:39:53 -0800, MK******@onebox.com (M. Katz) wrote:
I'm relatively new to PHP/MySQL and I've heard about maintaining a
persistent connection to the database. To save overhead, I'd like to
maintain the connection in a session variable, but I have a few
questions first.


Not dependant on if it works, I believe the overhead to connect to
MySQL is lower than keeping the session data on the webserver.

HTH, Jochen
--
Jochen Daum - Cabletalk Group Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.