PDO / 准备语句之间的区别 - PHP/MySQL

发布于 2024-11-06 04:00:22 字数 174 浏览 0 评论 0原文

我尝试 Google 寻找任何缺点,但没有发现任何缺点!
如果有人分享有关此主题的内容,我会很高兴!
PDO 和准备好的语句的优点/缺点

编辑 2:

我想每个人都想说 准备语句PDO 更好?
我说得对吗?

I tried Google to find any disadvantage but did not find any !
I'll be glad if anyone share some thing on this topic !
Advantage/ disadvantage of PDO and Prepared Statement

Edit 2 :

I think everyone want to say Prepared Statement is better than PDO ?
Am i right ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两相知 2024-11-13 04:00:22

PDO 的主要缺点是它需要更多的查询时间(我无法“证明”这一点,所以不要将其视为我注意到/读到的事实),这远少于一秒。但是,如果您需要额外的更少,那么第二个mysqli提供了像PDO这样的准备好的语句,并且我相信,考虑到它是为一个数据库而不是多个数据库设置的,工作速度会更快一些。

这是更多的代码,但正如 nikic 所说,使用包装类来设置您的语句,以便您的代码看起来像这样:

$db->fetchAll('SELECT * FROM table WHERE name = ?', $name);

其中 fetchAll 是您编写的用于处理参数绑定等的自定义函数。

我会用哪个? mysqliPDO (PDO 是因为它个人的多功能性)。 MySQL(不是mysqli)有点过时了,最后你可能会使用MySQL编写更多的代码,因为你总是有在将输入放入数据库之前过滤输入(就像转义以防止 SQL 注入一样,它不会验证应该放入其中的内容)。使用准备好的语句,过滤已经为您完成,只要使用得当,注入的机会就非常低。

The major disadvantage to PDO will be it takes a bit more querying time (I cannot "prove" this so don't take it as fact just what I have noticed / read), which is well less then a second. But if you need that extra less then a second mysqli offers prepared statements like PDO and I believe works a bit quicker given it is set for one database and not many.

It is more code, but as nikic stated, use a wrapper class to setup your statements so your code can look something like:

$db->fetchAll('SELECT * FROM table WHERE name = ?', $name);

Where fetchAll is a custom function you write to handle the binding of the params etc.

Which would I use? mysqli or PDO (PDO due to its versatility personally). MySQL (not mysqli) is kind of outdated, and in the end you could be writing a lot more code using MySQL in that you always have to filter the input (as in escape to prevent SQL injections, it will not validate what should be put in there) before putting it into the database. Using prepared statements, the filtering is all done for you and the chance for an Injection is very low as long as it is used properly.

2024-11-13 04:00:22

准备好的语句真正的一大优点是传递给 ? 的参数。检查有效性。所以 SQL 注入攻击比你创建这样的 sql 更难,

$SQL = "INSERT INTO table VALUES('" & $stringfromForm & "');";

就好像 somecone 可以设置在表单中输入以下内容

x'); INSERT INTO someothertable VALUES ('rubbish

那么你可能会遇到麻烦

The real big advantage of prepared statements is that the parameters passed in to the ? are checked for validity. So SQL injection attacks are harder than if you create your sql something like this

$SQL = "INSERT INTO table VALUES('" & $stringfromForm & "');";

as if somecone could set up enter the following into the form

x'); INSERT INTO someothertable VALUES ('rubbish

then you could be in trouble

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文