用PDO支持服务器端准备好的语句吗? [英] Support server side prepared statements with PDO?

查看:43
本文介绍了用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的内容为

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%'

我正在一个可憎的框架内工作(没有单元测试,没有文档,没有ryhme或原因),所以有人可能明确禁用了MySQL准备的语句,迫使PDO使用仿真模式.预期的行为?

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是PHP Version 5.2.10-2ubuntu6 MySQL的PDO驱动程序,客户端库版本5.1.41

PHP is PHP Version 5.2.10-2ubuntu6 PDO Driver for MySQL, client library version 5.1.41

更新: PDO()具有以下属性

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

我仔细阅读了PDO文档,但不幸的是,似乎没有提到像

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

推荐答案

某些PDO驱动程序不支持本机预处理语句,因此PDO会执行prepare的仿真.它还可以让您手动启用此仿真.

Some PDO drivers don't support native prepared statements, so PDO performs emulation of the prepare. It also lets you manually enable this emulation.

检查PDO::ATTR_EMULATE_PREPARES属性.当前的PDO手册中对此文档的记录很少.文献记载不充分的意思是,它在网站的注释中仅显示为"" ,而不在手册本身中显示.

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.

通常,您希望尽可能使用本机准备的语句.对于MySQL,如果要利用查询缓存,您实际上可能想在PDO中禁用本机准备好的语句! MySQL手册具有更多信息,但是简短版本是5.1.17之前的版本不会通过查询缓存运行准备好的语句,而后续版本仅在某些特定(但通用)条件下使用查询缓存.

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支持服务器端准备好的语句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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