Php zend Framework中的FetchList()不工作
我需要在zend frawork中运行一个查询。查询如下所示Php zend Framework中的FetchList()不工作,php,mysql,zend-framework,Php,Mysql,Zend Framework,我需要在zend frawork中运行一个查询。查询如下所示 select * from users where role_id in (3,5) and emailID not in ('abc@abc.com', 'cba@cba.com'); 我在传递绳子 $whereString = role_id in (3,5) and emailID not in ('abc@abc.com', 'cba@cba.com'); 到 但是fetchList()函数只执行(3,5)中的第一部分,即
select * from users where role_id in (3,5) and emailID not in ('abc@abc.com', 'cba@cba.com');
我在传递绳子
$whereString = role_id in (3,5) and emailID not in ('abc@abc.com', 'cba@cba.com');
到
但是fetchList()函数只执行(3,5)中的第一部分,即role\u id
但不是第二部分,即emailID不在abc@abc.com', 'cba@cba.com');代码>
fetchList()的结果包含'abc@abc.com“还有”cba@cba.com"
fetchList()是:
fetchAll():
任何人都知道我做错了什么。我有点冒险,因为我不确定。这感觉可能是一个报价问题。可以尝试传递查询字符串,如:
//use Zend_Db_Statement to process raw SQL.
//get default adapter
$db = Zend_Db_Table::getDefaultAdapter();
//write SQL statement
$sql = "select * from users where role_id in (3,5) and emailID not in ('abc@abc.com', 'cba@cba.com')";
//assuming MySql as DB instantiate object
$stmt = new Zend_Db_Statement_Mysqli($db, $sql);
//Execute SQL Statement accepts optional parameter for array() of values to be bound
$stmt->execute();
或者,您可能希望使用select()对象生成查询:
如果$whereString
是一个有效的PHP字符串,因为fetchAll()
接受一个字符串或Zend\u Db\u Select
的实例,那么它可能会起作用:
$whereString=“role\u id在(3,5)中,emailID不在('abc@abc.com', 'cba@cba.com')";
希望这对你有所帮助。。。我试了一下。@drew010更新了问题。请检查唯一看起来不合适的地方是$whereString不是正确引用的php字符串,但这可能只是因为问题的演示。您是否尝试过传递Zend_Db_Select的实例?@Liyali我认为emailID和role_id是表列。@RockyFord my bad。。。当我写下这句话时,我的思绪远在千里之外/@李亚丽,别担心,我一直都在做同样的事情。。。
public function fetchList($where=null, $order=null, $count=null, $offset=null)
{
$resultSet = $this->getDbTable()->fetchAll($where, $order, $count, $offset);
$entries = array();
foreach ($resultSet as $row)
{
$entry = new Application_Model_Users();
$entry->setId($row->id)
->setName($row->name)
->setEmail($row->email)
$entries[] = $entry;
}
return $entries;
}
/**
* Fetches all rows.
*
* Honors the Zend_Db_Adapter fetch mode.
*
* @param string|array|Zend_Db_Table_Select $where OPTIONAL An SQL WHERE clause or Zend_Db_Table_Select object.
* @param string|array $order OPTIONAL An SQL ORDER clause.
* @param int $count OPTIONAL An SQL LIMIT count.
* @param int $offset OPTIONAL An SQL LIMIT offset.
* @return Zend_Db_Table_Rowset_Abstract The row results per the Zend_Db_Adapter fetch mode.
*/
public function fetchAll($where = null, $order = null, $count = null, $offset = null)
{
if (!($where instanceof Zend_Db_Table_Select)) {
$select = $this->select();
if ($where !== null) {
$this->_where($select, $where);
}
if ($order !== null) {
$this->_order($select, $order);
}
if ($count !== null || $offset !== null) {
$select->limit($count, $offset);
}
} else {
$select = $where;
}
print_r($select);
$rows = $this->_fetch($select);
$data = array(
'table' => $this,
'data' => $rows,
'readOnly' => $select->isReadOnly(),
'rowClass' => $this->getRowClass(),
'stored' => true
);
$rowsetClass = $this->getRowsetClass();
if (!class_exists($rowsetClass)) {
require_once 'Zend/Loader.php';
Zend_Loader::loadClass($rowsetClass);
}
return new $rowsetClass($data);
}
//use Zend_Db_Statement to process raw SQL.
//get default adapter
$db = Zend_Db_Table::getDefaultAdapter();
//write SQL statement
$sql = "select * from users where role_id in (3,5) and emailID not in ('abc@abc.com', 'cba@cba.com')";
//assuming MySql as DB instantiate object
$stmt = new Zend_Db_Statement_Mysqli($db, $sql);
//Execute SQL Statement accepts optional parameter for array() of values to be bound
$stmt->execute();
//get default adapter
$db = Zend_Db_Table::getDefaultAdapter();
//instantiate the select() object
$select = new Zend_Db_Select($db);
$select->from('users');
//two where will build an AND query use $select->orWhere() to build an OR query
$select->where('role_id in (3,5)');
$select->where("emailID not in ('abc@abc.com', 'cba@cba.com')");
$result = $model->fetchList($select);