one connection for many pages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lipun4u
Forum Commoner
Posts: 82
Joined: Wed Jul 01, 2009 3:35 am
Location: Mumbai
Contact:

one connection for many pages

Post by lipun4u »

Is it possible to connect to database only once and use this connection across different pages ???
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: one connection for many pages

Post by Jade »

mehran
Forum Newbie
Posts: 17
Joined: Fri Jan 21, 2011 1:31 am

Re: one connection for many pages

Post 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,
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: one connection for many pages

Post 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.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: one connection for many pages

Post by Jade »

Hmm... I've never had those problems with it.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: one connection for many pages

Post 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.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: one connection for many pages

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: one connection for many pages

Post by John Cartwright »

And what about if you used normal mysql_connect(). Did you experience an issue with it?
lipun4u
Forum Commoner
Posts: 82
Joined: Wed Jul 01, 2009 3:35 am
Location: Mumbai
Contact:

Re: one connection for many pages

Post 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:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: one connection for many pages

Post 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.
Post Reply