Page 1 of 1

one connection for many pages

Posted: Wed Feb 02, 2011 3:47 pm
by lipun4u
Is it possible to connect to database only once and use this connection across different pages ???

Re: one connection for many pages

Posted: Wed Feb 02, 2011 4:09 pm
by Jade

Re: one connection for many pages

Posted: Thu Feb 03, 2011 6:39 am
by mehran
infact it is the real waye to make a connectin on one page and include that page every where you need it,
make a separate page with dababase connction and
as per you requirements include or require php builtin functions would be used,

Re: one connection for many pages

Posted: Thu Feb 03, 2011 9:30 am
by John Cartwright
I definitely wouldn't recommend using mysql_pconnect() unless you have a highly specific performance requirement to use it. Most of the time it will slow your connections down, because it doesn't really play well with Apache, and you'll end up with long lived mysql processes that aren't used and lots of new spawned processes occuring frequently.

Re: one connection for many pages

Posted: Thu Feb 03, 2011 10:23 am
by Jade
Hmm... I've never had those problems with it.

Re: one connection for many pages

Posted: Thu Feb 03, 2011 10:26 am
by John Cartwright
Jade wrote:Hmm... I've never had those problems with it.
Have you ever really looked at how it works, or carefully monitored your connections in a high traffic environment?

You will probably get away with it until you get lots of traffic.

Re: one connection for many pages

Posted: Thu Feb 03, 2011 10:28 am
by Jade
I get as many as 500 users during high traffic times and still no problems....but maybe that's not high enough traffic to cause an issue.

Re: one connection for many pages

Posted: Thu Feb 03, 2011 10:29 am
by John Cartwright
And what about if you used normal mysql_connect(). Did you experience an issue with it?

Re: one connection for many pages

Posted: Thu Feb 03, 2011 11:29 am
by lipun4u
what exactly i want is there will be connection from one session and the same connection will be used across different page requests in the same session.. :banghead:

Re: one connection for many pages

Posted: Thu Feb 03, 2011 12:08 pm
by John Cartwright
lipun4u wrote:what exactly i want is there will be connection from one session and the same connection will be used across different page requests in the same session.. :banghead:
Resources cannot be serialized, so no, you cannot store the connection for reuse across multiple pages. If you want to keep the connection open, and can re-connect to the open connection, then use persistent connections (but I don't recommend it).

I just think your trying to solve a problem that doesn't exist. Just use mysql_connect() normally on each page request.