In mysql, we will see that there are two commonly used database connection modes, one is a long-term connection, and the other is a connection that is disconnected after the page is accessed. Let me introduce the differences between mysql_connect and mysql_pconnect respectively. Friends who need to know more can refer to it.
PHP mysql_pconnect
The mysql_pconnect() function opens a persistent connection to the MySQL server.
mysql_pconnect() and mysql_connect() are very similar, but there are two main differences:
1. When connecting, this function will first try to find a (persistent) connection that has been opened on the same host with the same user name and password. If found, the connection ID will be returned without opening a new connection.
2. Secondly, the connection to the SQL server will not be closed when the script is executed, and the connection will remain open for future use (mysql_close() will not close the connection established by mysql_pconnect()).
Grammar
mysql_pconnect(server,user,pwd,clientflag) parameter description
server is optional. Specifies the server to connect to.
Can include a port number, such as "hostname:port", or a path to a local socket, such as ":/path/to/socket" for localhost.
If the PHP directive mysql.default_host is not defined (the default case), the default value is 'localhost:3306'.
user optional. username. The default value is the username of the server process owner.
pwd Optional. password. The default value is an empty password.
clientflag Optional. The client_flags parameter can be a combination of the following constants:
•MYSQL_CLIENT_SSL - Use SSL encryption
•MYSQL_CLIENT_COMPRESS - Use compression protocol
•MYSQL_CLIENT_IGNORE_SPACE - Allow space
after function name
•MYSQL_CLIENT_INTERACTIVE - allows interaction timeout inactivity time before closing connection
Return value
Returns a MySQL persistent connection identifier on success, or FALSE on error.
Tips and Notes
Note: The optional clientflag parameter is available since PHP version 4.3.0.
Tip: To create a non-persistent connection, use the mysql_connect() function.
Example
The code is as follows | Copy code |
代码如下 | 复制代码 |
$con = mysql_pconnect("localhost","mysql_user","mysql_pwd"); if (!$con) { die('Could not connect: ' . mysql_error()); } ?> |
PHP mysql_connect
The mysql_connect() function opens a non-persistent MySQL connection.
Grammar
mysql_connect(server,user,pwd,newlink,clientflag) parameter description
server is optional. Specifies the server to connect to.
Can include a port number, such as "hostname:port", or a path to a local socket, such as ":/path/to/socket" for localhost.
If the PHP directive mysql.default_host is not defined (the default case), the default value is 'localhost:3306'.
user optional. username. The default value is the username of the server process owner.
pwd Optional. password. The default value is an empty password.
newlink optional. If mysql_connect() is called a second time with the same parameters, no new connection will be established, but the ID of the already open connection will be returned. The parameter new_link changes this behavior and causes mysql_connect() to always open a new connection, even when mysql_connect() has been called previously with the same parameters.
clientflag Optional. The client_flags parameter can be a combination of the following constants:
•MYSQL_CLIENT_SSL - Use SSL encryption
•MYSQL_CLIENT_COMPRESS - Use compression protocol
•MYSQL_CLIENT_IGNORE_SPACE - Allow space
after function name
•MYSQL_CLIENT_INTERACTIVE - allows interaction timeout inactivity time before closing connection
Return value
Returns a MySQL connection ID on success, FALSE on failure.
Tips and Notes
Note: As soon as the script ends, the connection to the server is closed unless it has been explicitly closed previously by calling mysql_close().
Tip: To create a persistent connection, use the mysql_pconnect() function.
Example
The code is as follows | Copy code | ||||
|
The difference between mysql_connect and mysql_pconnect
The usage of these two functions is almost the same. Some people on the Internet say that pconnect should be used, and pconnect is a good tool; some people regard pconnect as a scourge and resolutely refuse to use pconnect, and some people have an unclear position. So what is this equipment like?
A permanent link does not mean that the server opens a connection and then everyone shares the link. Permanent connection also opens a connection for each client. If 200 people visit, there will be 200 connections. In fact, mysql_pconnect() itself does not do much processing. The only thing it does is not to automatically close the mysql connection after PHP stops running.
There is basically no difference between pconnect and connect when PHP is run through CGI. Because CGI is that each PHP accesses a process. When the access ends, the backward process will stop and all the data will be released. When PHP starts with When the apache module is running, because apache has an application process pool, an httpd process will be put back into the process pool after it is completed. This also prevents the mysql connection data opened with pconnect from being released, so when there is a next connection request It can be reused. This makes it possible that when the amount of concurrent access to Apache is not large, due to the use of pconnect, PHP saves the time of repeatedly connecting to the db, making the access speed faster. This should be easier to understand. But in Apache When the number of concurrent accesses is large, if you use pconnect, because the mysql connections occupied by some previous httpd processes are not closed, it may be because mysql has reached the maximum connection, so that some subsequent requests will never be satisfied. If the mysql maximum The number of connections is set to 500, and the maximum number of simultaneous accesses of apache is set to 2000. Assume that all accesses will request access to the db, and the operation time will be relatively long. The httpd requests for the current 500 requests will not stop, and the subsequent httpd process will not stop. It is impossible to connect to mysql (because the maximum number of mysql connections has been reached). Only the current 500 httpd processes can be stopped or reused to connect to mysql.
When the db operation is complex and takes a long time, httpd will fork many concurrent processes, and the httpd process that occurs first does not release the db connection, so that the httpd process that occurs later cannot connect to the db. Because this does not reuse other httpd processes MySQL connection process. As a result, many connection timeouts will occur. When the amount of concurrent access is not high, using pconnect can easily improve the access speed, but after the amount of concurrency increases, whether to use pconnect again depends on the developer's choice.
In my personal opinion, PHP's current connection to MySQL does not really use the connection pool. pconnect is just equivalent to borrowing the process pool of apache, so pconnect does not work very well when the number of concurrent accesses is large. Improve access to db efficiency.
In actual applications, if you use mysql_pconnect, each refresh and request for a new page will be faster, but if you use mysql_connect, you will have to re-request each time you refresh. When the database connection is relatively slow, you can see the difference. . When your database connection is relatively slow, the DB operation is not very complicated, and your program is confident enough that no locks will occur, or you have control over the server and meet any of the above four conditions. Two, then you can use pconnect.
pconnect does not need to be closed in the script. You can set the lifetime in mysql, or write a shell to scan regularly and kill connections that have been dormant for too long. One sentence summary: To make good use of pconnect, it is not only a matter of PHP scripts, but also related to the settings of the database and server.