在现有(旧版)应用程序中使用 PHP PDO。多个 SQL 连接?
我的任务是为现有的旧 PHP 应用程序编写一堆扩展。原来的应用程序是......呃......非常糟糕。它是 125K 行意大利面条式代码,其中散布着原始 SQL 语句并使用 mysql_*
函数。每天我都会找到真正属于 TheDailyWTF 的东西。到目前为止,我的计划是使新代码尽可能与旧代码分开。对于数据库访问,我计划使用 Idiorm 和 Paris,这是两个构建在 PDO 之上的非常好的小库。
但是我将如何处理数据库连接呢?或者 PHP 内部如何处理它们?我担心使用 PDO,每个页面调用都会打开两个单独的数据库连接。一种来自旧版 mysql_connect()
调用,另一种来自 PDO。这是真的吗?或者 PHP 会在后台简单地共享相同的连接吗?有没有办法让 PDO 和旧版 mysql_* 函数使用相同的数据库连接?我在每个页面加载时打开两个连接(注意:应用程序不使用事务)有什么关系吗?
我有点犹豫是否要检查这 125K 行意大利面条式代码并用其他东西替换所有 mysql_*
函数。我想尽可能少地接触原始代码,而不是在上面伤脑筋。
预先感谢您的任何建议。
I have been tasked to write a bunch of extensions to an existing legacy PHP app. The original app is... erm... quite bad. It's 125K lines of spaghetti code littered with raw SQL statements and using the mysql_*
functions. Every day I find things that really belong on TheDailyWTF. My plan so far is to keep my new code as separate as possible from the legacy code. For database access, I plan to use Idiorm and Paris, two really nice little libraries built on top of PDO.
But how will I handle database connections? Or how does PHP handle them internally? I worry that using PDO, every page call will open two separate connections to the database. One from the legact mysql_connect()
call and one from PDO. Is that true, or will PHP simply share the same connection in the background? Is there any way to make PDO and the legacy mysql_*
functions use the same database connection? Does it even matter at all it I opens two connections on every page load (note: the app is not using transactions)?
I'm a little hesitant about going over those 125K lines of spaghetti code and replacing all the mysql_*
functions with something else. I want to touch the original code as little as possible and not break my brain on it.
Thanks in advance for any advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将有两个连接,但不会破坏任何东西。请参阅此人提出同样问题的答案:
获取来自 mysql_connect() 的 PHP PDO 连接?
You will have two connections, but it's not going to break anything. See the answer to this person asking the very same question:
Getting a PHP PDO connection from a mysql_connect()?
这两个分机不会相互通信,因此您将以两个连接结束。这将是一项艰巨的工作,而且在我看来,很少有人会受益——只有那些处于你的处境、无法完全转变的人。
Those two extensions won't talk to each other and so you will end with two connections. That would be a hell lot of work and very few would benefit IMO -- only those in your shoes, who can't convert fully.