支持使用 PDO 的服务器端准备好的语句吗?
这样的
DB()->prepare("SELECT * FROM mysql.general_log WHERE user_host LIKE ?");
$statement->execute( array('%console%') );
foreach($statement as $record){
var_dump($record);
}
考虑到像General_log 的内容
*************************** 1. row ***************************
event_time: 2011-04-20 14:27:59
user_host: REDACTED[REDACTED] @ REDACTED [192.168.56.101]
thread_id: 30
server_id: 0
command_type: Connect
argument: REDACTED@REDACTED on REDACTED
*************************** 2. row ***************************
event_time: 2011-04-20 14:27:59
user_host: REDACTED[REDACTED] @ REDACTED [192.168.56.101]
thread_id: 30
server_id: 0
command_type: Query
argument: SELECT * FROM mysql.general_log WHERE user_host LIKE '%console%'
内容,我正在一个令人厌恶的框架内工作(没有单元测试,没有文档,没有韵律或原因),所以是否有可能有人明确禁用 MySQL 准备好的语句,迫使 PDO 使用模拟模式。或者这是预期的行为?
PHP 是 PHP 版本 5.2.10-2ubuntu6 MySQL 的 PDO 驱动程序,客户端库版本 5.1.41
更新: PDO() 是使用以下属性构造的
PDO::ATTR_PERSISTENT => false
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
,我浏览了 PDO 文档,但不幸的是,似乎没有提到像 这个类似的问题
Given something like
DB()->prepare("SELECT * FROM mysql.general_log WHERE user_host LIKE ?");
$statement->execute( array('%console%') );
foreach($statement as $record){
var_dump($record);
}
Contents of general_log is
*************************** 1. row ***************************
event_time: 2011-04-20 14:27:59
user_host: REDACTED[REDACTED] @ REDACTED [192.168.56.101]
thread_id: 30
server_id: 0
command_type: Connect
argument: REDACTED@REDACTED on REDACTED
*************************** 2. row ***************************
event_time: 2011-04-20 14:27:59
user_host: REDACTED[REDACTED] @ REDACTED [192.168.56.101]
thread_id: 30
server_id: 0
command_type: Query
argument: SELECT * FROM mysql.general_log WHERE user_host LIKE '%console%'
I'm working inside of an abomination framework ( no unit-tests, no documentation, no ryhme or reason ) so is it possible that somewhere someone explicitly disabled MySQL prepared statements forcing PDO to use emulated mode... or is this expected behavior?
PHP is PHP Version 5.2.10-2ubuntu6
PDO Driver for MySQL, client library version 5.1.41
Update:
PDO() is constructed with the the following attributes
PDO::ATTR_PERSISTENT => false
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
I went through the PDO documentation, but unfortunately there doesn't seem to be mention of a flag like the one written about in this similar question
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
某些 PDO 驱动程序不支持本机准备好的语句,因此 PDO 执行准备的模拟。它还允许您手动启用此模拟。
检查
PDO::ATTR_EMULATE_PREPARES
属性。当前 PDO 手册中对此的记录很少。我所说的文档记录不足是指它仅出现在网站的评论中,而不是出现在手册本身中。通常,您希望尽可能使用本机准备好的语句。对于 MySQL,如果您正在利用查询缓存,您可能实际上想要禁用 PDO 中的本机准备语句! MySQL 手册有更多信息,但是简而言之,5.1.17 之前的版本不会通过查询缓存运行准备好的语句,后续版本仅在某些特定(但常见)条件下使用查询缓存。
(有些人建议完全关闭查询缓存。使用 大型缓存大小实际上可能会对性能造成重大影响。)
Some PDO drivers don't support native prepared statements, so PDO performs emulation of the prepare. It also lets you manually enable this emulation.
Check the
PDO::ATTR_EMULATE_PREPARES
attribute. It's poorly documented in the current PDO manual. By poorly documented, I mean that it appears only in comments on the site, not in the manual itself.Generally you want to use native prepared statements whenever possible. In the case of MySQL, if you are taking advantage of the query cache, you might actually want to disable native prepared statements in PDO! The MySQL manual has more information, but the short version is that versions prior to 5.1.17 don't run prepared statements through the query cache, and subsequent versions only use the query cache under certain specific (but common) conditions.
(Some people recommend turning off the query cache entirely. Using large cache sizes can actually be a major performance hit.)
默认情况下,PDO_MYSQL 模拟准备好的语句。
要使用本机服务器端准备好的语句,应该明确设置
By default, PDO_MYSQL emulates the prepared statements.
To use the native server-side prepared statements, one should explicitely set