如何防止PDO将问号解释为占位符? [英] How to prevent PDO from interpreting a question mark as a placeholder?
问题描述
要检测 hstore 中密钥的存在,我需要运行查询像这样:
For detecting the existence of a key in a hstore, I need to run a query like this:
SELECT * FROM tbl WHERE hst ? 'foo'
但是,这给了我一个PDOException:
However, that gives me a PDOException:
PDOException: SQLSTATE[HY093]: Invalid parameter number: no parameters were bound: SELECT * FROM tbl WHERE hst ? 'foo'
是否有任何方法可以逃脱问号,以便PDO不会将其用作占位符?我尝试使用最多四个反斜杠以及一个双问号(??
),但似乎没有什么说服PDO留下问号的.
Is there any way to escape the question mark so PDO won't pick it up as a placeholder? I've tried with up to four backslashes, as well as a double question mark (??
), but nothing seems to persuade PDO to leave the question mark alone.
推荐答案
使用函数调用表单.根据系统目录,hstore ?
运算符使用exist
函数:
Use the function call form. According to the system catalogs, the hstore ?
operator uses the exist
function:
regress=# select oprname, oprcode from pg_operator where oprname = '?';
oprname | oprcode
---------+---------
? | exist
(1 row)
因此您可以写:
SELECT * FROM tbl WHERE exist(hst,'foo');
(个人而言,我不是hstore以操作员为中心的设计和文档的忠实拥护者,我认为它放弃了基于函数的接口的有用的自文档化属性,而没有任何真正的好处,我通常使用其函数调用而不是其运算符.仅仅因为您可以定义运算符并不意味着您应该这样做.)
(Personally I'm not a big fan of hstore's operator-centric design and documentation, I think it discards the useful self-documenting properties of a function based interface without any real benefit and I usually use its function calls rather than its operators. Just because you can define operators doesn't mean you should.)
这篇关于如何防止PDO将问号解释为占位符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!