我正在使用 Zend Framework 1.12.3。我需要创建一个嵌套 WHERE 的查询,例如
SELECT * FROM table1
WHERE (id = 'foo' AND name = 'bar') OR (grade = 123)
这是我的尝试
$this->getDbTable()->select()
->from($this->getDbTable(), array('*')
->where('id = ? AND name = ?', $foo, $bar)
->orWhere('grade = ?', $grade);
然而,结果却是
SELECT * FROM table1
WHERE (id = 'foo' AND name = 'foo') OR (grade = 123)
代替 name = 'bar'
基本上,我不能使用多个 ?
为每个 ?
分配不同的值。你知道任何解决方案吗?
谢谢
LE:使用 WHERE 条件,例如 ->where("id = $foo and name = $bar")
确实有效,但是它不能像 这样防止注入(inject)攻击?
确实
最佳答案
查看代码,我看不出有什么办法,where 子句仅适用于条件,但我认为添加括号,您可以使用正确的优先级管理 AND 和 OR。
试试这个:
this->getDbTable()->select()
->from($this->getDbTable(), array('*')
->where('(id = ?', $foo) // add '(' before condition
->where('name = ?)', $bar) // add ')' after condition
->orWhere('grade = ?', $grade);
关于php - Zend Framework 1.12 Db Select - 嵌套 WHEREs 在条件中使用问号的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24776743/