使用 PHP 时 MySQL 与 MySQLi
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/548986/
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
MySQL vs MySQLi when using PHP
提问by anand.trex
Which is better, MySQL or MySQLi? And why? Which should I use?
MySQL 和 MySQLi 哪个更好?为什么?我应该使用哪个?
I mean better not just in terms of performance, but any other relevant feature.
我的意思是不仅在性能方面更好,而且在任何其他相关功能方面都更好。
采纳答案by Mark Davidson
If you have a look at MySQL Improved Extension Overview, it should tell you everything you need to know about the differences between the two.
如果您查看MySQL 改进的扩展概述,它应该会告诉您有关两者之间差异的所有信息。
The main useful features are:
主要有用的功能是:
- an Object-oriented interface
- support for prepared statements
- support for multiple statements
- support for transactions
- enhanced debugging capabilities
- embedded server support.
- 面向对象的接口
- 支持准备好的报表
- 支持多语句
- 交易支持
- 增强的调试能力
- 嵌入式服务器支持。
回答by Gordon
There is a manual page dedicated to help choosing between mysql, mysqli and PDO at
有一个专门帮助在 mysql、mysqli 和 PDO 之间进行选择的手册页位于
- http://php.net/manual/en/mysqlinfo.api.choosing.phpand
- http://www.php.net/manual/en/mysqlinfo.library.choosing.php
- http://php.net/manual/en/mysqlinfo.api.choosing.php和
- http://www.php.net/manual/en/mysqlinfo.library.choosing.php
The PHP team recommends mysqli or PDO_MySQL for new development:
PHP 团队推荐使用 mysqli 或 PDO_MySQL 进行新开发:
It is recommended to use either the mysqli or PDO_MySQL extensions. It is not recommended to use the old mysql extension for new development. A detailed feature comparison matrix is provided below. The overall performance of all three extensions is considered to be about the same. Although the performance of the extension contributes only a fraction of the total run time of a PHP web request. Often, the impact is as low as 0.1%.
建议使用 mysqli 或 PDO_MySQL 扩展。新开发不建议使用旧的mysql扩展。下面提供了详细的特征比较矩阵。所有三个扩展的整体性能被认为大致相同。尽管扩展的性能只占 PHP Web 请求总运行时间的一小部分。通常,影响低至 0.1%。
The page also has a feature matrix comparing the extension APIs. The main differences between mysqli and mysql API are as follows:
该页面还具有比较扩展 API 的功能矩阵。mysqli 和 mysql API 的主要区别如下:
mysqli mysql
Development Status Active Maintenance only
Lifecycle Active Long Term Deprecation Announced*
Recommended Yes No
OOP API Yes No
Asynchronous Queries Yes No
Server-Side Prep. Statements Yes No
Stored Procedures Yes No
Multiple Statements Yes No
Transactions Yes No
MySQL 5.1+ functionality Yes No
* http://news.php.net/php.internals/53799
* http://news.php.net/php.internals/53799
There is an additional feature matrix comparing the libraries (new mysqlnd versus libmysql) at
有一个额外的功能矩阵比较库(新的 mysqlnd 与 libmysql)在
and a very thorough blog article at
和一篇非常详尽的博客文章
回答by cletus
I have abandoned using mysqli. It is simply too unstable. I've had queries that crash PHP using mysqli but work just fine with the mysql package. Also mysqli crashes on LONGTEXT columns. This bug has been raised in various forms since at least 2005 and remains broken. I'd honestly like to use prepared statements but mysqli just isn't reliable enough (and noone seems to bother fixing it). If you really want prepared statements go with PDO.
我已经放弃使用 mysqli。简直太不稳定了。我有使用 mysqli 使 PHP 崩溃的查询,但使用 mysql 包工作得很好。mysqli 在 LONGTEXT 列上也崩溃了。至少自 2005 年以来,此错误已以各种形式提出,并且仍然被破坏。老实说,我很想使用准备好的语句,但 mysqli 不够可靠(似乎没有人愿意修复它)。如果您真的想要准备好的语句,请使用 PDO。
回答by Ross
MySQLi stands for MySQL improved. It's an object-oriented interface to the MySQL bindings which makes things easier to use. It also offers support for prepared statements (which are veryuseful). If you're on PHP 5 use MySQLi.
MySQLi 代表 MySQL 改进。它是一个面向对象的 MySQL 绑定接口,使事情更容易使用。它还提供对准备好的语句(非常有用)的支持。如果您使用的是 PHP 5,请使用 MySQLi。
回答by MarkR
What is better is PDO; it's a less crufty interface and also provides the same features as MySQLi.
更好的是 PDO;它是一个不那么粗糙的界面,并且还提供与 MySQLi 相同的功能。
Using prepared statements is good because it eliminates SQL injection possibilities; using server-side prepared statements is bad because it increases the number of round-trips.
使用准备好的语句很好,因为它消除了 SQL 注入的可能性;使用服务器端准备好的语句是不好的,因为它增加了往返次数。
回答by Javier
for me, prepared statements is a must-have feature. more exactly, parameter binding (which only works on prepared statements). it's the only really sane way to insert strings into SQL commands. i really don't trust the 'escaping' functions. the DB connection is a binary protocol, why use an ASCII-limited sub-protocol for parameters?
对我来说,准备好的语句是必备功能。更准确地说,参数绑定(仅适用于准备好的语句)。这是将字符串插入 SQL 命令的唯一真正理智的方法。我真的不相信“转义”功能。DB连接是二进制协议,为什么要使用ASCII限制的参数子协议?

