tags:

views:

99

answers:

3

I've tried restoring connections in $_SESSION,

but can only reuse for one user.

Any better solutions?

+5  A: 

What do you mean be "reuse connections"? A connection is a socket being opened to the server..of course you can't store it in $_SESSION. It still would have to reconnect, so it would be pointless anyways. The closest you can come to it is persistent connections.

ryeguy
reuse means:not close the connection after each request.So next request will save time.
Shore
Well persistent connections do that. You still have to specify the connection like you normally would though; there is no other solution.
ryeguy
+3  A: 

Read about mysql_pconnect().

Unfortunately, persistent connection is not stable in PDO and not supported by MySQLi.

Persistent connections save time when the database server is not in the localhost, but if it is, use of persistent connections is practically irrelevant.

Havenard
Also, if you are using PDO you can do something like:$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(PDO::ATTR_PERSISTENT => true));
Kitson
According to the PDO documentation, it is not entirely safe or recommended.
Havenard
A: 

use mysql_pconnect rather than mysql_connect

nont