Mysql_connect v/s Mysql_pconnect

Source: Internet
Author: User

Original: mysql_connect v/s mysql_pconnect

Translation: Mysql_connect v/s mysql_pconnect

Translator: Dwqs

When you need to connect to MySQL database using PHP, you will find that there are two kinds of database connection functions: mysql_connect () and Mysql_pconnect ().

1, mysql_connect ()

At the beginning of the script, mysql_connect () creates a new connection and closes the connection at the end of the script. Each time the script executes, it will create a new connection.

2, Mysql_pconnect ()

When using Mysql_pconnect () to connect to a database, it first determines whether a database connection already exists, and if so, it takes advantage of the resource ID that is used first, and instead creates a new connection.

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, when the script ends, the connection is not closed. This is called a persistent connection.

However, when using Mysql_pconnect (), you need to fine-tune the server configuration, such as restricting the number of connections, timing out connections, and handling empty connections.

3. How to use these two functions

If your app has a lot of traffic, you should use the Mysql_pconnect () function instead of 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 there is no advantage to persistent connections in either case.

4, use Mysql_pconnect () should pay attention to things

In general, when you lock a table, the connection is automatically unlocked after it is closed. However, because the persistent connection does not close, any table that you accidentally lock will remain locked, and the only way to unlock it is to wait for the connection to time out or to kill the process manually. This problem can also be caused when dealing with things.

Similarly, temporary tables are deleted after the connection is closed. However, because the persistent connection does not close, the temporary table is no longer a temporary table. If you do not delete the staging table after you complete the requirement, it will be connected as a new client and is for the same connection. The same problem is caused when setting the session variable.

The effect of using persistent connections on Apache is not very good. When Apache receives a request from the client, it opens a new database connection instead of opening the database with an existing persistent connection, which results in many idle processes, wasting resources, and an unknown error when the maximum number of connections is reached.

Original starting: http://www.ido321.com/1312.html

Next: Dom Notes (a): HTMLDocument interface


Mysql_connect v/s Mysql_pconnect

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.