Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysqli persistent connection

Tags:

php

mysql

mysqli

In short, is there some sort of mysqli_pconnect for high-usage PHP & MySQL servers, or do I need to stick with mysql unimproved? And if so, why did they remove it?

like image 683
Aaron Yodaiken Avatar asked Aug 13 '10 18:08

Aaron Yodaiken


People also ask

Do I need to close MySQLi connection?

Explicitly closing open connections and freeing result sets is optional. However, it's a good idea to close the connection as soon as the script finishes performing all of its database operations, if it still has a lot of processing to do after getting the results.

Which function gives persistent connection with database?

mysql_pconnect() acts very much like mysql_connect() with two major differences. First, when connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.

What is Mysql_pconnect () function?

mysql_pconnect() function This function establishes a connection with the database when the script is called. This function first checks whether a connection with the same username and password is created or not and if not then it establishes a connection.


2 Answers

Support for this was introduced in PHP 5.3. For versions before this, PDO and – god forbid – the mysql extension are the only options.

To quote the manual:

Unlike the mysql extension, mysqli does not provide a separate function for opening persistent connections. To open a persistent connection you must prepend p: to the hostname when connecting.

like image 173
Artefacto Avatar answered Sep 25 '22 13:09

Artefacto


I don't bother with persistent connections in MySQL. Persistent connections are for databases like Oracle, in which making a new connection is much slower.

In MySQL, making a connection is relatively fast (especially if you turn off reverse DNS lookups). There should be no need for persistent connections with MySQL. If your app is performance-critical, there are tons of things you can do with greater bang for your buck than worrying about persistent connections.

Besides, persistent connections come with unintended side effects. If you use MySQL variables, temporary tables, change the character set of a connection, or forget to finish transactions, you could cause problems. For example, you could expose one user's private data to another user's PHP session because you leave a temporary table alive.

like image 39
Bill Karwin Avatar answered Sep 24 '22 13:09

Bill Karwin