Mysql_connect v/s mysql_pconnect, mysqlpconnect
Original article: mysql_connect v/s mysql_pconnect
Translation: mysql_connect v/s mysql_pconnect
Translator: dwqs
When you need to use PHP to connect to the MySQL database, you will find two database connection functions: mysql_connect () and mysql_pconnect ().
1. mysql_connect ()
At the beginning of the script, mysql_connect () will create a new connection and close the connection at the end of the script. Each time the script is executed, it creates a new connection.
2. mysql_pconnect ()
When mysql_pconnect () is used to connect to a database, it first determines whether a database connection exists. If yes, it uses the resource ID first. Otherwise, A new connection is created.
After the connection is created, the function returns the resource ID (if any ). When the script is called again, it does not create a new connection. Of course, the connection will not be closed when the script ends. This is called persistent connection.
However, when using mysql_pconnect (), you need to fine-tune the server configuration, such as limiting connections, timeout connections, and handling empty connections.
3. How to use these two functions
If your application has a high traffic volume, use the mysql_pconnect () function. Otherwise, consider the mysql_connect () function.
If PHP and MySQL are on the same server or on the local network, the number of connections can be ignored, because in either case, persistent connections have no advantage.
4. What should I pay attention to when using mysql_pconnect ()?
Generally, when you lock a table, the connection is automatically unlocked after it is closed. However, because persistent connections are not closed, any tables that you accidentally lock will remain locked. The only way to unlock is to wait for the connection to time out or manually kill the process. This problem also occurs during transaction processing.
Similarly, the temporary table will be deleted after the connection is closed. However, since the persistent connection is not closed, the temporary table is no longer a temporary table. If you do not delete the temporary table after the requirement is met, it will be connected as a new client and is for the same connection. The same problem occurs when setting Session variables.
Using persistent connections on Apache does not work well. When Apache receives a request from the client, it opens a new database connection instead of using a persistent connection to open the database. This causes many idle processes and resources to be wasted, an unknown error occurs when the maximum number of connections is reached.
First: http://www.ido321.com/1312.html
Next article: DOM note (1): HTMLDocument Interface