简体   繁体   English

PDO,Mysql和本机准备语句

[英]PDO, Mysql and native prepared statements

The understanding of PDO that I've had up till now is that PDO will use real prepared statements where it can, and emulate them where it can't. 到目前为止,我对PDO的理解是PDO将在可能的地方使用真正的预备语句,并在不可以的地方模拟它们。 I also understood that where mysql was concerned, real prepared statements would be used provided you were communicating with a version of mysql that supported them. 我还了解到,在涉及mysql的情况下,如果您正在与支持它们的mysql版本进行通信,则将使用真正的预处理语句。

In fact the PHP manual page for the MySQL PDO driver says as much. 实际上,MySQL PDO驱动程序的PHP手册页说明了很多。 http://php.net/manual/en/ref.pdo-mysql.php http://php.net/manual/zh/ref.pdo-mysql.php

However, on another SO question I was helping out on How to replace all occurencies of char with elements from array? 但是,在另一个SO问题上,我在帮助如何用数组元素替换char的所有出现? , someone made the comment that this is not true, and that in fact PDO emulates prepared statements when talking to a MySQL database. ,有人评论说这是不正确的,事实上,PDO在与MySQL数据库对话时会模拟准备好的语句。

I've not had much luck finding evidence to back the claims made there up, but I did discover that there is a PDO::ATTR_EMULATE_PREPARES attribute that can be configured to toggle prepared statement emulation. 我没有太多运气找到证据来支持提出的声明,但是我确实发现有一个PDO :: ATTR_EMULATE_PREPARES属性可以配置为切换准备好的语句仿真。

so what's the truth of the matter? 那么事情的真相是什么? Does PDO really not use prepared statements with mysql? PDO真的不与mysql一起使用准备好的语句吗? If not, can it be forced to do so, and if so, should you do that? 如果不是,是否可以强迫这样做?如果这样做,您应该这样做吗? I've always assumed that as mysql has real prepared statements that PDO would use them, as claimed in the manual. 我一直认为,正如手册中所声称的那样,由于mysql具有PDO将使用它们的真正准备好的语句。 Is the manual inaccurate? 手册不正确吗?

EDIT TO ADD : I have read a few articles recently that at least give a decent rationale as to why real prepared queries are disabled by default. 编辑 :最近我阅读了几篇文章,至少对合理地禁用实际准备好的查询为何提供了合理的理由。 PHP scripts tend to be short lived and only run long enough to generate a response to a request, after which all their resources are deallocated. PHP脚本的生存期很短,只能运行很长时间才能生成对请求的响应,然后将其所有资源释放。 For any query you only execute once per request you'd actually have to execute 2 SQL commands (The prepare command and the execute command) so for any prepared statement that's only executed once you'll probably actually get slightly worse performance than with the emulated prepared statement. 对于任何查询,每个请求仅执行一次,实际上您必须执行2条SQL命令(prepare命令和execute命令),因此对于仅执行一次的任何Prepared语句,您实际上可能会获得比模拟结果稍差的性能。准备好的声明。 For queries that must be run in a loop real prepared statements will probably perform better but such cases are relatively rare in the kind of request/response model PHP is typically used for. 对于必须在循环中运行的查询,实际的预准备语句可能会更好地执行,但是这种情况在通常用于PHP的请求/响应模型中相对较少。

I still think real prepared statements are preferable but at least I have a reasonable explanation as to why PDO uses emulated queries by default. 我仍然认为最好使用真正的预备语句,但至少我对PDO为什么默认使用模拟查询有一个合理的解释。

Does PDO really not use prepared statements with mysql? PDO真的不与mysql一起使用准备好的语句吗?

Yes, by default (at least with version I tested) but native mode can be turned on manually. 是的,默认情况下(至少使用我测试过的版本),但是纯模式可以手动打开。

If not, can it be forced to do so 如果没有,是否可以强迫这样做

By employing PDO::ATTR_EMULATE_PREPARES setting, the name is pretty self-explanatory. 通过使用PDO::ATTR_EMULATE_PREPARES设置,该名称非常不言自明。

$dbh->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );

should you do that? 你应该那样做吗?

That's hardest question of them all. 这是所有人中最困难的问题。
Well, I'd say - yes, you should. 好吧,我会说-是的,你应该。 If you choose PDO as your db driver, there is no point in using it in the emulation mode. 如果选择PDO作为数据库驱动程序,则在仿真模式下使用它是没有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM