When to use mySQL_pconnect() in PHP

Asked By 0 points N/A Posted on -
qa-featured

When to use mysql_pconnect()? How it is different from mysql_connect?

SHARE
Best Answer by timang88
Best Answer
Best Answer
Answered By 0 points N/A #84303

When to use mySQL_pconnect() in PHP

qa-featured

mysql_pconect()  performs just like mysql_connect().  However, they still have their differences.

They have two major differences:
First, when it is in the state of connecting, it tries to look for a (persistent) link that's already open with same host, username and password. If the function has been able to found one, an identifier of it will be returned than by opening a new connection.
 
Second, the connection to the SQL server will not be closed even when the execution of the script ends. It will still be left open for possible use in the future. Even the function mysql_close() will not close links created by  mysql_pconnect().
 
mysql_pconnect() is called 'persistent'.
 
Persistent Database Connection – these are links that do not close the executions when your script ends. If a persistent connection is asked, PHP checks if there is an existing identical persistent connection  (that has stayed open from earlier)   and if the persistent connection does exist from before, it will be used. If none found, it creates the link.  
 
As a matter of fact, this persistent connection does not have the ability to open 'user sessions' on the link, this do not have the ability to create a transaction efficiently and this do not do a lot of other things.
 
The nice thing about this persistent connection is that they are good if the overhead  to create a link to your SQL server is high.  They let the child to simply connect only once of its entire life, than by letting it connect to the SQL server every time it  processes a page. Meaning, every child that opened a persistent connection will have its own open persistent connection to the server.
Answered By 0 points N/A #84304

When to use mySQL_pconnect() in PHP

qa-featured

Hi

mysql_connect() and mysql_pconnect() both are used for database connection but with little difference. In mysql_pconnect(), ‘p’ stands for persistence connection.

When we use mysql_connect() function, every time it will open and close MySQL connection depending on the request where as when we use mysql_pconnect() function,
The function would try to find a (persistent) connection that is already opened with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.

Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the connection will remain open for future use (mysql_close() will not close connection established by mysql_pconnect()).

Thanks & Regards

Ben

Login/Register to Answer

Related Questions