PHP PDO 和 MySQLi [英] PHP PDO and MySQLi

查看:21
本文介绍了PHP PDO 和 MySQLi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚完成了 PHP 入门课程,在整个 stackoverflow 论坛中,人们都建议我改用 PDO、准备好的语句或 MYSQLi,我简单地查看了手册,但大部分内容都超出了我的脑海.

I just finished an introduction course in PHP, and throughout the stackoverflow forum people have recommended that I switch to PDO, prepared statements or MYSQLi, I briefly checked the manual but most of it went over my head.

到目前为止我一直在使用 mysql_* 函数,所以这些概念对我来说是新的.我认为它们用于访问和执行特定于数据库的操作,但我不确定.

I've been using mysql_* functions up till now so these concepts are new to me. I think they are used to access and perform database specific actions, but I'm not sure.

那么PDO、准备语句和MySQLi 之间有什么区别,它们是完成相同任务的不同功能吗?它们在脚本中兼容还是选择一个"?最后,哪个性能最好?

So what is the difference between PDO, prepared statements and MySQLi, are they different features that accomplishes the same task? Are they compatible in a script or is it "choose one or the other"? And lastly which offers the best performance?

更新:感谢您的回答,我将寻找更多 PDO 教程.

Update: Thanks for the answers, I'll be hunting for more PDO tutorials.

作为参考,我还发现以下帖子很有用:

For reference I also found the following posts useful:

哪个又快又轻 - mysqli 或 PDO

mysqli 或 PDO - 优缺点是什么?

推荐答案

在基本层面上,mysql、mysqli 和 PDO 扩展都回答了我如何与数据库对话? 他们都提供了连接到数据库并从中发送和检索数据的函数和功能.您可以同时使用它们,同时与数据库建立多个连接,但这通常是无稽之谈.

At the basic level the mysql, mysqli and PDO extensions all answer the question how do I talk to the database? They all provide functions and functionality to connect to a database and send and retrieve data from it. You can use them all at the same time establishing several connections to the database at once, but that's typically nonsense.

mysql* 是一个非常简单的扩展,它基本上允许您连接到数据库,向其发送 SQL 查询,而不是其他.
mysqli 通过添加参数化查询和其他一些东西来改进这一点(顾名思义).
PDO 是一种将多个数据库驱动程序抽象为一个包的扩展,即它允许您使用相同的代码连接到 MySQL、Oracle、MS SQL Server 和许多其他数据库,而无需使用特定于数据库的扩展或在以下情况下重写您的代码你切换数据库(至少在理论上).它还支持参数化查询.

mysql* is a very simple extension that basically allows you to connect to the database, send it SQL queries and not much else.
mysqli improves this (as the name suggests) by adding parameterized queries and a few other things into the mix.
PDO is an extension that abstracts several database drivers into one package, i.e. it allows you to use the same code to connect to MySQL, Oracle, MS SQL Server and a number of other databases without needing to use database specific extensions or rewrite your code when you switch databases (in theory at least). It also supports parameterized queries.

如果您知道将专门使用 MySQL,那么 mysqli 是一个不错的选择.特别是因为您可以以程序方式使用它,您已经从 mysql 扩展中习惯了它.如果您不熟悉 OOP,那会很有帮助.否则,PDO 是一个不错的面向对象、灵活的数据库连接器.

If you know you're going to be using MySQL exclusively, mysqli is a good choice. Especially since you can use it in a procedural way, what you're already used to from the mysql extension. If you're not familiar with OOP, that's helpful. Otherwise, PDO is a nice object oriented, flexible database connector.

* 请注意,mysql 扩展现在不推荐使用 并将在未来某个时候删除.那是因为它是古老的,充满了不好的做法并且缺乏一些现代特征.不要用它来编写新代码.

* Note that the mysql extension is now deprecated and will be removed sometime in the future. That's because it is ancient, full of bad practices and lacks some modern features. Don't use it to write new code.

这篇关于PHP PDO 和 MySQLi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆