php 我什么时候应该使用 MySQLi 而不是 MySQL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8891443/
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
When should I use MySQLi instead of MySQL?
提问by Semere Taézaz Sium
Can someone clarify for me what the advantages and disadvantages of using MySQLi instead of MySQL? Are there situations where I should not use MySQLi? Do I need to configure my server differently in order to use MySQLi? For instance, do I need to upgrade Apache or PHP so I can support MySQLi?
有人可以为我澄清使用 MySQLi 而不是 MySQL 的优点和缺点吗?是否存在我不应该使用 MySQLi 的情况?我是否需要以不同的方式配置我的服务器才能使用 MySQLi?例如,我是否需要升级 Apache 或 PHP 才能支持 MySQLi?
回答by Repox
Reasons why you should use MySQLi extension instead of the MySQL extension are many:
您应该使用 MySQLi 扩展而不是 MySQL 扩展的原因有很多:
- MySQLi gives you prepared statements - a safer way of sending data to MySQL and protecting you from SQL injection. This alone should be enough for always choosing MySQLi over MySQL.
- MySQLi enables most of the MySQL features.
- MySQLi is object orientated.
- MySQLi supports prepared statements, transactions and multiple statements.
- The old MySQL extension is deprecated as of PHP 5.5.0
- MySQLi 为您提供准备好的语句 - 一种将数据发送到 MySQL 并保护您免受 SQL 注入的更安全的方式。仅此一项就足以让您始终选择 MySQLi 而不是 MySQL。
- MySQLi 启用了大部分 MySQL 功能。
- MySQLi 是面向对象的。
- MySQLi 支持预处理语句、事务和多语句。
- 自 PHP 5.5.0 起不推荐使用旧的 MySQL 扩展
And there are other benefits. But mainly, you should focus on security and stabiltity - and MySQLi gives you just that.
还有其他好处。但主要是,您应该关注安全性和稳定性——而 MySQLi 正是如此。
回答by Your Common Sense
PHP team refuse to support mysql extension any further. This reason is alone enough.
PHP 团队拒绝进一步支持 mysql 扩展。单凭这个理由就够了。
All other reasons don't make much sense:
所有其他原因都没有多大意义:
- MySQLi gives you prepared statements- one can use old mysql with manually handled plaeholders and get even safer. This alone should be stopping MySQL from useless deprecation.
- MySQLi enables most of the MySQL featureswhich most of PHP users never ever heard of.
- MySQLi is object oriented- a pair of straight hands can make old mysql objec oriented in a matter of several hours.
- *MySQLi supports
- transactions* - mysql supports them as well. just run
START TRANSACTION
query and you're set. - multiple statements- yet nobody actually needs them.
- prepared statements- as a matter of fact, the support is horrible, renders them practiaclly unusable
- transactions* - mysql supports them as well. just run
- MySQLi 为您提供准备好的语句——您可以使用带有手动处理的 plaeholders 的旧 mysql并变得更加安全。仅此一项就可以阻止 MySQL 无用的弃用。
- MySQLi 启用了大多数PHP 用户从未听说过的 MySQL 功能。
- MySQLi 是面向对象的——一双直手可以在几个小时内使旧的 mysql 面向对象。
- *MySQLi 支持
- 交易* - mysql 也支持它们。只需运行
START TRANSACTION
查询即可。 - 多个语句- 但实际上没有人需要它们。
- 准备好的语句- 事实上,支持是可怕的,使它们实际上无法使用
- 交易* - mysql 也支持它们。只需运行
So, there are no advantages at all.
If you want to get along with non-deprecated but usable extension - go for the PDO.
所以,完全没有优势。
如果您想使用未弃用但可用的扩展程序 - 选择PDO。
回答by J0HN
If you are using mysql
> 4.1.3. Mysqli is a new interface for mysql in php. A quote from http://www.php.net/manual/en/mysqli.overview.php
如果您使用的是mysql
> 4.1.3。mysqli 是 php 中 mysql 的新接口。引自http://www.php.net/manual/en/mysqli.overview.php
What is PHP's MySQL Extension?
PHP 的 MySQL 扩展是什么?
This is the original extension designed to allow you to develop PHP applications that interact with a MySQL database. The mysql extension provides a procedural interface and is intended for use only with MySQL versions older than 4.1.3. This extension can be used with versions of MySQL 4.1.3 or newer, but not all of the latest MySQL server features will be available.
这是最初的扩展,旨在允许您开发与 MySQL 数据库交互的 PHP 应用程序。mysql 扩展提供了一个过程接口,仅适用于 4.1.3 之前的 MySQL 版本。此扩展可用于 MySQL 4.1.3 或更高版本,但并非所有最新的 MySQL 服务器功能都可用。
Note:
If you are using MySQL versions 4.1.3 or later it is strongly recommended that you
use the mysqli extension instead.
回答by Sjoerd
Always, if possible.
总是,如果可能的话。
It is possible that MySQLi is not supported by your PHP install. However, most hosting providers have support for MySQLi.
您的 PHP 安装可能不支持 MySQLi。但是,大多数托管服务提供商都支持 MySQLi。
回答by Altrozero
Unless you are using an old version of the MySQL database (prior to 4.1.3) or require some functionality not yet included in MySQLi (I think there are one of two functions that weren't moved over) then stick with MySQLi when ever you can.
除非您使用的是旧版本的 MySQL 数据库(4.1.3 之前的版本)或需要一些尚未包含在 MySQLi 中的功能(我认为有两个功能中的一个没有被移过)然后坚持使用 MySQLi能够。