Mysql_connect v/s mysql_pconnect, mysqlpconnect

Source: Internet
Author: User
Tags php and mysql

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.