php MySQL、MySQLi 和 PDO 之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2190737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:25:46  来源:igfitidea点击:

What is the difference between MySQL, MySQLi and PDO?

phpmysqlpdomysqli

提问by Chintan Parekh

What is the difference between MySQL, MySQLiand PDO?

MySQL、MySQLiPDO之间有什么区别?

Which one is the best suited to use with PHP-MySQL?

哪一种最适合与 PHP-MySQL 一起使用?

回答by Matthew Flaschen

There are (more than) three popular ways to use MySQL from PHP. This outlines some features/differences PHP: Choosing an API:

有(超过)三种流行的方式从 PHP 使用 MySQL。这概述了一些特性/差异PHP:选择 API

  1. (DEPRECATED) The mysql functionsare procedural and use manual escaping.
  2. MySQLiis a replacement for the mysql functions, with object-oriented and procedural versions. It has support for prepared statements.
  3. PDO(PHP Data Objects) is a general database abstraction layer with support for MySQL among many other databases. It provides prepared statements, and significant flexibility in how data is returned.
  1. (已弃用mysql 函数是程序性的,并且使用手动转义。
  2. MySQLi是 mysql 函数的替代品,具有面向对象和过程版本。它支持准备好的语句。
  3. PDO(PHP 数据对象)是一个通用数据库抽象层,支持 MySQL 以及许多其他数据库。它提供了准备好的语句,以及数据返回方式的极大灵活性。

I would recommend using PDO with prepared statements. It is a well-designed API and will let you more easily move to another database (including any that supports ODBC) if necessary.

我建议将 PDO 与准备好的语句一起使用。它是一个精心设计的 API,如果需要,可以让您更轻松地移动到另一个数据库(包括任何支持ODBC 的数据库)。

回答by RageZ

Those are different APIs to access a MySQL backend

这些是访问 MySQL 后端的不同 API

  • The mysqlis the historical API
  • The mysqliis a new version of the historical API. It should perform better and have a better set of function. Also, the API is object-oriented.
  • PDO_MySQL, is the MySQL for PDO. PDO has been introduced in PHP, and the project aims to make a common API for all the databases access, so in theory you should be able to migrate between RDMS without changing any code (if you don't use specific RDBM function in your queries), also object-oriented.
  • MySQL的是历史API
  • mysqli的是历史API的新版本。它应该表现更好,并具有更好的功能集。此外,API 是面向对象的。
  • PDO_MySQL,是 PDO 的 MySQL。PDO 已经在 PHP 中引入,该项目旨在为所有数据库访问创建一个通用 API,因此理论上您应该能够在不更改任何代码的情况下在 RDMS 之间迁移(如果您在查询中不使用特定的 RDBM 函数) ),也是面向对象的。

So it depends on what kind of code you want to produce. If you prefer object-oriented layers or plain functions...

所以这取决于你想要生成什么样的代码。如果您更喜欢面向对象的层或普通函数...

My advice would be

我的建议是

  1. PDO
  2. MySQLi
  3. mysql
  1. PDO
  2. MySQLi
  3. mysql

Also my feeling, the mysql API would probably being deleted in future releases of PHP.

同样我的感觉是,mysql API 可能会在PHP.

回答by Sarfraz

mysqliis the enhanced version of mysql.

mysqli是 mysql 的增强版。

PDO extensiondefines a lightweight, consistent interface for accessing databases in PHP. Each database driver that implements the PDO interface can expose database-specific features as regular extension functions.

PDO 扩展定义了一个轻量级、一致的接口,用于在 PHP 中访问数据库。每个实现 PDO 接口的数据库驱动程序都可以将特定于数据库的功能作为常规扩展功能公开。

回答by Sarfraz

Specifically, the MySQLi extension provides the following extremely useful benefits over the old MySQL extension..

具体来说,与旧的 MySQL 扩展相比,MySQLi 扩展提供了以下非常有用的好处。

OOP Interface (in addition to procedural) Prepared Statement Support Transaction + Stored Procedure Support Nicer Syntax Speed Improvements Enhanced Debugging

OOP 接口(除了过程)准备好的语句支持事务 + 存储过程支持更好的语法速度改进增强的调试

PDO Extension

PDO 扩展

PHP Data Objects extension is a Database Abstraction Layer. Specifically, this is not a MySQL interface, as it provides drivers for many database engines (of course including MYSQL).

PHP 数据对象扩展是一个数据库抽象层。具体来说,这不是 MySQL 接口,因为它为许多数据库引擎(当然包括 MYSQL)提供了驱动程序。

PDO aims to provide a consistent API that means when a database engine is changed, the code changes to reflect this should be minimal. When using PDO, your code will normally "just work" across many database engines, simply by changing the driver you're using.

PDO 旨在提供一致的 API,这意味着当数据库引擎更改时,反映这一点的代码更改应该最少。使用 PDO 时,您的代码通常会在许多数据库引擎中“正常工作”,只需更改您使用的驱动程序即可。

In addition to being cross-database compatible, PDO also supports prepared statements, stored procedures and more, whilst using the MySQL Driver.

除了跨数据库兼容之外,PDO 还支持准备好的语句、存储过程等,同时使用 MySQL 驱动程序。