从mysql_connect()获取PHP PDO连接? [英] Getting a PHP PDO connection from a mysql_connect()?

查看:65
本文介绍了从mysql_connect()获取PHP PDO连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旧版PHP/MySQL应用程序,该应用程序调用mysql_connect().现有的大量下游代码使用此连接直接或通过包装程序进行mysql_query()调用.

I have a legacy PHP/MySQL app that calls mysql_connect(). Tons of existing downstream code makes mysql_query() calls, either directly or through wrappers, using this connection.

对于我在应用程序上开发的新代码,我想开始使用PDO.

For new code that I develop on the app, I would like to start using PDO.

如果我使用相同的主机/用户/密码/dbname凭据建立PDO连接,那么我是否很幸运,在幕后,PHP将重新使用原始连接?还是PHP会创建到服务器的两个不同的连接(虽然完全可以理解,但这是不希望的)?

If I make a PDO connection using the same host/user/pass/dbname credentials, might I be so lucky that under the hood, PHP will re-use the original connection? Or will PHP create two distinct connections to the server (undesirable, albeit totally understandable)?

谢谢!

推荐答案

如果您使用两种不同的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秒钟后消失)

这篇关于从mysql_connect()获取PHP PDO连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆