1, when the function mysql_connect the first three parameters ( server
username
password)相同,并且第四个参数(new_link)不传递时候
,重复调用
mysql_connect will return the same connection.
PHP code
<? PHP $db mysql_connect (' localhost ', ' root ', ' root '); Var_dump ($db); $db 2 mysql_connect (' localhost ', ' root ', ' root '); Var_dump ($db 2); Sleep (10);
Page printing Information
Number of MySQL connections
Mysql> Show Full processlist;+----+------+-----------------+------+---------+------+-------+------------------ -----+| Id | User | Host | db | Command | Time | State | Info |+----+------+-----------------+------+---------+------+-------+-----------------------+| | root | localhost:54408 | NULL | Query | 0 | NULL | show full Processlist | | | | root | localhost:55278 | NULL | Sleep | 3 | | NULL |+----+------+-----------------+------+---------+------+-------+-----------------------+2 rows in Set
2, when the function mysql_connect passed the fourth parameter ( new_link
), and then repeated calls it.
PHP code
<? PHP $db mysql_connect (' localhost ', ' root ', ' root ', 1); Var_dump ($db); $db 2 mysql_connect (' localhost ', ' root ', ' root ', 1); Var_dump ($db 2); Sleep (10);
Page printing Information
Number of MySQL connections
Mysql> Show Full processlist;+----+------+-----------------+------+---------+------+-------+------------------ -----+| Id | User | Host | db | Command | Time | State | Info |+----+------+-----------------+------+---------+------+-------+-----------------------+| | root | localhost:54408 | NULL | Query | 0 | NULL | show full Processlist | | | | root | localhost:55320 | NULL | Sleep | 4 | | NULL | | | | root | localhost:55321 | NULL | Sleep | 4 | | NULL |+----+------+-----------------+------+---------+------+-------+-----------------------+
3. Mysql_pconnect Persistent Connection
PHP code
<? PHP $db Mysql_pconnect (' localhost ', ' root ', ' root '); Var_dump ($db); $db 2 Mysql_pconnect (' localhost ', ' root ', ' root '); Var_dump ($db 2);
Page printing Information
Number of MySQL connections
Mysql> Show Full processlist;+----+------+-----------------+------+---------+------+-------+------------------ -----+| Id | User | Host | db | Command | Time | State | Info |+----+------+-----------------+------+---------+------+-------+-----------------------+| 1 | Root | localhost:55391 | NULL | Query | 0 | NULL | show full Processlist | | 2 | Root | localhost:55393 | NULL | Sleep | 5 | | NULL |+----+------+-----------------+------+---------+------+-------+-----------------------+
4, Mysql_pconnect plus the fourth parameter (New_link) can establish multiple persistent connections
PHP code
<? PHP $db Mysql_pconnect (' localhost ', ' root ', ' root ', 1); Var_dump ($db); $db 2 Mysql_pconnect (' localhost ', ' root ', ' root ', 1); Var_dump ($db 2);
Page printing Information
Number of MySQL connections
Mysql> Show Full processlist;+----+------+-----------------+------+---------+------+-------+------------------ -----+| Id | User | Host | db | Command | Time | State | Info |+----+------+-----------------+------+---------+------+-------+-----------------------+| 1 | Root | localhost:55391 | NULL | Query | 0 | NULL | show full Processlist | | 2 | Root | localhost:55393 | NULL | Sleep | | | NULL | | 3 | Root | localhost:55418 | NULL | Sleep | 4 | | NULL |+----+------+-----------------+------+---------+------+-------+-----------------------+3 rows in Set
5. Summary:
The persistent connection created by the function mysql_pconnetc after the PHP script ends, the MySQL connection does not end immediately. When it ends, it is managed by MySQL itself. The Mysql_colse function cannot close the MYSQL_PCONNETC connection.
The connection created by the function mysql_connetc will end when the PHP script ends, and the MySQL connection ends accordingly.
PHP about MySQL Long connection problem