MySQL connection management in PHP

Source: Internet
Author: User
Note: When mysql_connect () is used to obtain shared MySQL connection resources, calling mysql_close () sometimes does not release MySQL connections.

Note: When mysql_connect () is used to obtain shared MySQL connection resources, calling mysql_close () sometimes does not release MySQL connections.

MySQL is a scarce resource for PHP programs. By default, the maximum number of connections for MySQL is 100, that is, a maximum of 100 MySQL clients can be connected. Therefore, PHP has made some optimizations when using MySQL connections. These optimizations may improve performance, but may also cause some confusion when using MySQL connections.

Create a MySQL connection:

Resource mysql_connect ([string server [, string username [, string password [, bool new_link [, int client_flags])

After mysql_connect () is used to create a MySQL connection, PHP stores the connection information, including host, username, and password, and connection resources. When mysql_connect () is called next time, PHP will check whether there is a connection resource with the same parameters (including host, username, password) by default. If yes, it will directly return the previously created resource, instead of creating a new MySQL connection. (PS: When two connections are not the same server, only new MySQL connections can be created, and old connections cannot be shared)

PHP will bypass this check and directly initiate a MySQL connection request to the PHP server only when new_link is manually specified as true.

In this case, an error occurs:

The user expects to query table1 information in database1. However, due to the second mysql_connect (), PHP returns the last MySQL connection resource. Therefore, switching to a database in $ conn2 also affects $ conn1. $ conn1 also switches to database database2 by default. The queried information is the result on database2.

To solve this problem, set new_link to true when mysql_connect () is called for the second time.

Close the MySQL connection:

For connection resources created using mysql_connect (), unset resource variables () cannot actually release resources. You need to call mysql_close () to release MySQL connection resources.

Note that when mysql_connect () is used to obtain shared MySQL connection resources, calling mysql_close () sometimes does not release MySQL connections. Only when all shared connection resources call mysql_close, PHP will actually release the MySQL connection.

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.