Log in

View Full Version : Difference between mysql_pconnect() and mysql_connect()



letom
11-15-2013, 04:27 PM
Why it is impossible to to use mysql_pconnect() instead of mysql_connect() to get the maximum result of caching,
mysql_pconnect() Establishes a persistent connection to a MySQL server. (http://ir.php.net/function.mysql-pconnect) then why mostly prefer mysql_connect() ?

traq
11-15-2013, 06:13 PM
First and foremost,

# If at all possible, you should avoid using the mysql_* functions. #
Existing code should be updated to avoid performance and security problems. ext/mysql is outdated and has not been a recommended "best practice" since 2004.


Warning
This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future.
Instead, either the mysqli (http://php.net/mysqli) or PDO_MySQL (http://php.net/PDO) extension should be used.
See also the MySQL API Overview (http://php.net/mysqlinfo.api.choosing) for further help while choosing a MySQL API.

As for persistent connections,

They are still available, of course, when you use MySQLi or PDO, and you can use them if you want to. However, persistent connections don't always carry the benefits you expect them to. To start with, under Apache, a new process is usually spawned per request anyway, so there is no opportunity to re-use the connection. It will simply sit, idle, in its original process until Apache decides to kill it. In the meantime, MySQL is still devoting resources to it, and it's still in the connection pool so it still counts towards your maximum number of connections. If you have a moderately busy site (or a sudden spike in activity; maybe a hundred or so visitors at once), you can easily lock out your DB server.

Also, to clarify, persistent connections have nothing to do with "caching." They operate directly on the DB, like any other connection.

Read more (php.net/mysql_pconnect#85670).