我是否应该将我的网站升级到PHP MySQLi或PDO?

时间:2022-10-19 22:19:10

I have designed a website before 5 years using PHP MySQL and still works fine without any issues. I heard MySQL is officially deprecated as of PHP 5.5 and has been removed as of PHP 7. Also, PHP offers three different APIs to connect to MySQL (like MySQL, MySQLi, and PDO). My web server is updated frequently.

我使用PHP MySQL在5年前设计了一个网站,但仍然没有任何问题。我听说MySQL从PHP 5.5开始正式被弃用,并且从PHP 7开始被删除。此外,PHP提供了三种不同的API来连接MySQL(如MySQL,MySQLi和PDO)。我的Web服务器经常更新。

I understood, I have to move to newer API like MySQLi or PDO for safety. Now I am confused whether to choose MySQLi or PDO. Also, Is there any compatibility/migrating options available for such case?

我明白了,为了安全起见,我必须转向更新的API,如MySQLi或PDO。现在我很困惑是选择MySQLi还是PDO。此外,是否有适用于此类情况的兼容性/迁移选项?

3 个解决方案

#1


2  

The answer is fairly simple.

答案很简单。

If, like majority of PHP users, you are going to use database API functions right in the application code, without any intermediate wrapper, then PDO is your only choice, as it's a sort of wrapper already, automating many operations that with mysqli have to be done manually.

如果像大多数PHP用户一样,你将在应用程序代码中使用数据库API函数,而没有任何中间包装器,那么PDO是你唯一的选择,因为它已经是一种包装器,使mysqli必须自动完成许多操作手动完成。

No, there are no migration options, because the very approach is changed dramatically: instead of placing variables right in the query, they have to be substituted in the query with special marks. There is no way to automate this process.

不,没有迁移选项,因为这种方法发生了巨大变化:不是在查询中直接放置变量,而是必须在查询中用特殊标记替换它们。没有办法自动化这个过程。

#2


4  

Lets take a look at both of these extensions.

让我们来看看这两个扩展。

PDO

PDO

PDO is database neutral - You only need to learn one API to work with dozens of databases

PDO是数据库中立的 - 您只需要学习一个API来处理数十个数据库

So if you decide to move to another database, the only thing you would be changing is the DSN(data source name).

因此,如果您决定转移到另一个数据库,您唯一要改变的是DSN(数据源名称)。

Support of both name and '?' placeholders for prepared statements.

支持名称和'?'预备陈述的占位符。

Drupal uses PDO.

Drupal使用PDO。

Mysqli

库MySQLi

On the other hand Mysqli is designed specifically for Mysql databases and is recommended by Mysql. Mysqli works with Mysql and MariaDB databases.

另一方面,Mysqli是专为Mysql数据库设计的,是Mysql推荐的。 Mysqli适用于Mysql和MariaDB数据库。

Support of only '?' placeholders for prepared statements.

仅支持'?'预备陈述的占位符。

Joomla uses Mysqli

Joomla使用Mysqli

Conclusion

结论

There are many conflicting arguments weather Mysqli or PDO runs faster. Both Mysqli and PDO use the same underlying drivers to access the database making the performance not worth comparing.

天气Mysqli或PDO运行得更快,存在许多相互矛盾的争论。 Mysqli和PDO都使用相同的底层驱动程序来访问数据库,这使得性能不值得比较。

So there is no clear winner when working with Mysqli or PDO. But let's say you use Mysqli and then later you want to use another database, it would be a difficult transition.

所以在使用Mysqli或PDO时没有明显的赢家。但是让我们说你使用Mysqli,然后你想使用另一个数据库,这将是一个艰难的过渡。

The main strength of PDO over Mysqli is name placeholders for prepared statements and which is why I chose PDO over Mysqli.

PDO相对于Mysqli的主要优势是预备语句的名称占位符,这也是我选择PDO而不是Mysqli的原因。

#3


-2  

PDO and MySQli both are used for database connection and both have their own advantage. In closer look PDO wins the battle but if you really stick with only one database provider then my personal best choice is use MySQLi.

PDO和MySQli都用于数据库连接,两者都有自己的优势。仔细看看PDO赢得了战斗,但如果你真的坚持只有一个数据库提供商,那么我个人的最佳选择就是使用MySQLi。

You will also extract some good points from: http://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059

您还将从以下内容中提取一些优点:http://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059

#1


2  

The answer is fairly simple.

答案很简单。

If, like majority of PHP users, you are going to use database API functions right in the application code, without any intermediate wrapper, then PDO is your only choice, as it's a sort of wrapper already, automating many operations that with mysqli have to be done manually.

如果像大多数PHP用户一样,你将在应用程序代码中使用数据库API函数,而没有任何中间包装器,那么PDO是你唯一的选择,因为它已经是一种包装器,使mysqli必须自动完成许多操作手动完成。

No, there are no migration options, because the very approach is changed dramatically: instead of placing variables right in the query, they have to be substituted in the query with special marks. There is no way to automate this process.

不,没有迁移选项,因为这种方法发生了巨大变化:不是在查询中直接放置变量,而是必须在查询中用特殊标记替换它们。没有办法自动化这个过程。

#2


4  

Lets take a look at both of these extensions.

让我们来看看这两个扩展。

PDO

PDO

PDO is database neutral - You only need to learn one API to work with dozens of databases

PDO是数据库中立的 - 您只需要学习一个API来处理数十个数据库

So if you decide to move to another database, the only thing you would be changing is the DSN(data source name).

因此,如果您决定转移到另一个数据库,您唯一要改变的是DSN(数据源名称)。

Support of both name and '?' placeholders for prepared statements.

支持名称和'?'预备陈述的占位符。

Drupal uses PDO.

Drupal使用PDO。

Mysqli

库MySQLi

On the other hand Mysqli is designed specifically for Mysql databases and is recommended by Mysql. Mysqli works with Mysql and MariaDB databases.

另一方面,Mysqli是专为Mysql数据库设计的,是Mysql推荐的。 Mysqli适用于Mysql和MariaDB数据库。

Support of only '?' placeholders for prepared statements.

仅支持'?'预备陈述的占位符。

Joomla uses Mysqli

Joomla使用Mysqli

Conclusion

结论

There are many conflicting arguments weather Mysqli or PDO runs faster. Both Mysqli and PDO use the same underlying drivers to access the database making the performance not worth comparing.

天气Mysqli或PDO运行得更快,存在许多相互矛盾的争论。 Mysqli和PDO都使用相同的底层驱动程序来访问数据库,这使得性能不值得比较。

So there is no clear winner when working with Mysqli or PDO. But let's say you use Mysqli and then later you want to use another database, it would be a difficult transition.

所以在使用Mysqli或PDO时没有明显的赢家。但是让我们说你使用Mysqli,然后你想使用另一个数据库,这将是一个艰难的过渡。

The main strength of PDO over Mysqli is name placeholders for prepared statements and which is why I chose PDO over Mysqli.

PDO相对于Mysqli的主要优势是预备语句的名称占位符,这也是我选择PDO而不是Mysqli的原因。

#3


-2  

PDO and MySQli both are used for database connection and both have their own advantage. In closer look PDO wins the battle but if you really stick with only one database provider then my personal best choice is use MySQLi.

PDO和MySQli都用于数据库连接,两者都有自己的优势。仔细看看PDO赢得了战斗,但如果你真的坚持只有一个数据库提供商,那么我个人的最佳选择就是使用MySQLi。

You will also extract some good points from: http://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059

您还将从以下内容中提取一些优点:http://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059