Page 1 of 1
MySQL connection question
Posted: Sun May 22, 2011 9:42 am
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
Re: MySQL connection question
Posted: Mon May 23, 2011 1:20 am
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.
Re: MySQL connection question
Posted: Thu Jul 21, 2011 2:55 am
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()?
Re: MySQL connection question
Posted: Thu Jul 21, 2011 3:11 am
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.