MySQL connection question

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

MySQL connection question

Post by MindOverBody »

I am making some lightweight MySQL connector so I am interested which way to go:

Case A:
Connect and disconnect from MySQL server on every query

Case B:
Keep connection alive and disconnect when connection is no longer needed

Which case is better by your opinion?

Thanks,
Bojan
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: MySQL connection question

Post by Christopher »

Generally in PHP, connections are closed at the end of every request. So it makes the most sense to open a connection and let PHP close it. There are persistent connections, but they can cause more problems than they solve.
(#10850)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: MySQL connection question

Post by social_experiment »

The Manual wrote:In fact, to be extremely clear about the subject, persistent connections don't give you any functionality that wasn't possible with their non-persistent brothers.
If you open a connection using mysql_connect() and use it throughout the script (open connection when script starts and let php close it for you) is that a persistent connection? Or is this term (persistent connection) only applicable to connections opened with mysql_pconnect()?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: MySQL connection question

Post by Christopher »

My understanding is that using mysql_pconnect() or related call gives persistent connections. Also, I have read that these sometimes give better performance and sometimes give worse performance that regular connections.
(#10850)
Post Reply