php - Zend Framework 1.12 Db Select - 嵌套 WHEREs 在条件中使用问号的多个值

标签 php zend-framework zend-db-select

我正在使用 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/

相关文章:

php - getDbTable()->select()->where 问题 (Zend Framework 1.x)

sql - 将 Zend Db Select 查询转换为 sql 查询

php - PHP 正则表达式中的 "range out of order in character class"?

php - 通过 PHP SDK 以用户身份发布到 Facebook 粉丝页面提要

php - Zend 框架 : Require one out of three values

php - 关于 Php、MySQL、Magento 和 Ubuntu 服务器

zend-framework - 如何使用 Zend_Db_Select 加入子查询

mysql - Zend Db Select 中带有 JOINLEFT 的 GROUP_CONCAT

javascript - AngularJs + php 身份验证

PHP Mcrypt - 解密函数不适用于一小部分字符串