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.