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
MySQL connection question
Moderator: General Moderators
- MindOverBody
- Forum Commoner
- Posts: 96
- Joined: Fri Aug 06, 2010 9:01 pm
- Location: Osijek, Croatia
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: MySQL connection question
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)
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: MySQL connection question
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()?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.
“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
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: MySQL connection question
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)