php pdo 准备重复变量
问题描述
在编写 pdo 语句时,是否可以重复变量的值?我的意思是:
While writing a pdo statement, is it possible to repeat the value of a variable? I mean:
请注意,我重复了 ":name" 名称,而我只提供了一次该值.我怎样才能做到这一点?
Please note that I repeat the ":name" nameholder whereas I provide the value only once. How can I make this work?
推荐答案
简单的答案是:你不能.PDO 对准备好的语句使用抽象,但有一些限制.不幸的是,这是一个,您必须使用类似
The simple answer is: You can't. PDO uses an abstraction for prepared statements which has some limitations. Unfortunately this is one, you have to work-around using something like
在某些情况下,例如使用某些版本的 PDO/MySQL 驱动程序模拟准备好的语句,支持重复命名参数;但是,不应依赖它,因为它很脆弱(例如,它会使升级需要更多工作).
In certain cases, such as emulated prepared statements with some versions of the PDO/MySQL driver, repeated named parameters are supported; however, this shouldn't be relied upon, as it's brittle (it can make upgrades require more work, for example).
如果你想支持一个命名参数的多次出现,你总是可以扩展 PDO 和 PDOStatement(通过经典继承或通过组合),或者只是 PDOStatement 并通过设置 PDO::ATTR_STATEMENT_CLASS
属性将您的类设置为语句类.扩展的 PDOStatement(或 PDO::prepare
)可以提取命名参数,查找重复并自动生成替换.它还会记录这些重复项.bind 和 execute 方法在传递命名参数时会测试该参数是否重复并将值绑定到每个替换参数.
If you want to support multiple appearances of a named parameter, you can always extend PDO and PDOStatement (by classical inheritance or by composition), or just PDOStatement and set your class as the statement class by setting the PDO::ATTR_STATEMENT_CLASS
attribute. The extended PDOStatement (or PDO::prepare
) could extract the named parameters, look for repeats and automatically generate replacements. It would also record these duplicates. The bind and execute methods, when passed a named parameter, would test whether the parameter is repeated and bind the value to each replacement parameter.
注意:以下示例未经测试,可能存在错误(一些与语句解析相关的在代码注释中注明).
Note: the following example is untested and likely has bugs (some related to statement parsing are noted in code comments).
这篇关于php pdo 准备重复变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!