简体   繁体   English

PHP:从mysql_connect()PDO重用开放的MySQL连接?

[英]PHP: Reuse Open MySQL Connection from mysql_connect() PDO?

I am just curious to see if there is a way to transfer or re-use a connection opened by mysql_connect() to PDO. 我只是想知道是否有办法将mysql_connect()打开的连接转移或重新使用到PDO。 I know this is incorrect (hoping to get the sort of idea across), but i was hoping for something like 我知道这是不正确的(希望得到这种想法),但我希望有类似的东西

$pdo = new PDO($mysql_connect_link); // yes this is wrong, i know!

for example. 例如。

The reason is that I want to use PDO in my class to manage queries and such, however, the class itself doesn't open the connection to the database. 原因是我想在我的类中使用PDO来管理查询等,但是,类本身不会打开与数据库的连接。 It requires a connection to be established beforehand (which I have been checking via the standard mysql_* () function set for now. Ew, I know.) 它需要事先建立连接(我现在通过标准的mysql_* ()函数进行检查。我知道。我知道。)

I would like for the class's queries to work regardless on the user opening a connection to the database via mysql_* or PDO ... Does anyone have any thoughts and/or suggestions on this matter? 无论用户是通过mysql_* 还是 PDO打开与数据库的连接,我都希望类的查询能够正常工作......有没有人对此事有任何想法和/或建议?

You should just port your mysql_ functions to PDO. 您应该将mysql_函数移植到PDO。

$pdo = new PDO($mysql_connect_link);

Is wrong because of the parameters it takes (see: http://php.net/manual/fr/pdo.construct.php ), but yes technically you could have both connections running side by side. 由于它所采用的参数是错误的(参见: http//php.net/manual/fr/pdo.construct.php ),但从技术上讲,你可以让两个连接并排运行。 Although, no, you obviously can't run PDO queries with a run of the mill mysql_connect. 虽然,不,你显然无法运行磨机mysql_connect运行PDO查询。

I am just curious to see if there is a way to transfer a connection opened by mysql_connect() to PDO. 我只是想知道是否有办法将mysql_connect()打开的连接转移到PDO。

NO 没有

I would like for the class's queries to work regardless on the user opening a connection to the database via mysql_* or PDO... 无论用户是通过mysql_ *还是PDO打开与数据库的连接,我都希望类的查询能够正常工作...

It's quite hard to understand your reasons. 理解你的理由很难。

If you want to keep both drivers, you have to keep 2 sets of functions as well. 如果你想保留两个驱动程序,你还必须保留两组功能。 And completely change the class structure. 并彻底改变了班级结构。 You have to have an abstract class with common methods and 2 implementations for them. 你必须有一个带有常用方法的抽象类和2个实现。 No connections passed to the class but class itself have to manage connection. 没有连接传递给类,但类本身必须管理连接。 I doubt it worth such a labor though. 我怀疑它值得这样的劳动。

If you just want to allow use of your class along with some legacy mysql_* code - just open another connection from PDO. 如果您只是想允许使用您的类以及一些遗留的mysql_ *代码 - 只需从PDO打开另一个连接。 It's not such an evil thing to be avoided at any cost. 不惜一切代价避免这样的恶事。 It's just a connection. 这只是一个联系。 It will burden your server slightly, yes, but it will be least problem for the application that uses such a sandwiched spaghetti. 它会给您的服务器带来轻微负担,是的,但对于使用这种夹心意大利面条的应用来说,这将是最不重要的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM