且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从mysql_connect()获取PHP PDO连接?

更新时间:2023-02-26 10:15:26

如果您使用两种不同的API(即mysql_*和PDO),PHP将生成两个不同的连接.

If you are using two different APIs (i.e. mysql_* and PDO), PHP will generate two different connections.


并且,作为证明",请考虑以下部分代码:


And, as a "proof", consider this portion of code :

$db = mysql_connect('localhost', 'USER', 'PASSWORD');
$pdo = new PDO('mysql://@localhost/astralblog', 'USER', 'PASSWORD');
sleep(5);


运行此命令将在MySQL服务器上导致两个截然不同的连接-它将休眠5秒钟:


Running this will cause two distinct connections, on the MySQL server -- which will sleep for 5 seconds :

mysql> show processlist;
+----+------------+-----------------+------------+---------+------+-------+------------------+
| Id | User       | Host            | db         | Command | Time | State | Info             |
+----+------------+-----------------+------------+---------+------+-------+------------------+
| 41 | astralblog | localhost:46551 | astralblog | Sleep   |  188 |       | NULL             |
| 42 | astralblog | localhost:46552 | astralblog | Sleep   |  188 |       | NULL             |
| 43 | astralblog | localhost       | astralblog | Query   |    0 | NULL  | show processlist |
| 64 | astralblog | localhost       | NULL       | Sleep   |    4 |       | NULL             |
| 65 | astralblog | localhost       | NULL       | Sleep   |    4 |       | NULL             |
+----+------------+-----------------+------------+---------+------+-------+------------------+
5 rows in set (0,00 sec)

(所讨论的连接是最后两个,当我启动PHP脚本时出现,并在5秒钟后消失)