Zend framework Zend Framework查询不起作用?
我对Zend SELECT中的SELECT语句有问题Zend framework Zend Framework查询不起作用?,zend-framework,Zend Framework,我对Zend SELECT中的SELECT语句有问题 public function listAnswers($sort_field = 'answer_id', $field = null, $value = null, $strict_filter = false, $client_id = null) { //here is $value // $value = "abcd : <?\\?>"; $value = $this->getDefau
public function listAnswers($sort_field = 'answer_id', $field = null, $value = null, $strict_filter = false, $client_id = null) {
//here is $value
// $value = "abcd : <?\\?>";
$value = $this->getDefaultAdapter()->quote("%".$value."%");
if( !empty($field) && !empty($value) && $strict_filter == false){
$select = $this->select()->where(" client_id != -99 ")->where($field . " like $value ")->order($sort_field);
}
}
错误来了,我的查询被打印出来了
SELECT `answer`.* FROM `answer` WHERE ( client_id != -99 ) AND (client_id = '1') AND (answer_text LIKE '%abcd : <?\\\\?>%' ) ORDER BY `add_date` DESC
记录不符合$value的要求。根据评论中的讨论,您认为它只是在浏览器中显示SQL查询,而不是显示字符串的“第1部分”,因为浏览器会将字符解释为HTML标记并隐藏其内容。用于查询本身的代码看起来很好,因此只需使用:
echo htmlspecialchars($select);exit;
用于调试以验证是否正在运行正确的查询。使用select时通常不需要引用值,select通常会默认提供引用。 使用“选择”时,最好使用占位符而不是连接,因为连接可能并不总是有效的 $select->order需要一个字符串来指定查询“answer\u id ASC”,除非您总是想要默认值 默认情况下,您将$field和$value设置为null,因此测试!空真的没有语义意义 不要忘记,您必须实际获取结果并返回它
//assuming a dbTable model where $this->select() is valid.
public function listAnswers($sort_field = 'answer_id', $field = null, $value = null, $strict_filter = false, $client_id = null) {
//here is $value
// $value = "abcd : <?\\?>";
//unless your values are really strange select() will provide the quotes
//$value = $this->getDefaultAdapter()->quote("%".$value."%");
//it may help to initialize select() before the loop
$select = $this->select();
if( !is_null($field) && !is_null($value) && $strict_filter == false){
$select->where(" client_id != -99 ")
//use placeholders when using the select()
->where("$field like ?" $value)
//specify order by with a string ie: answer_id ASC
->order($sort_field . 'ASC');
}
//return the result
return $this->fetchAll($select)
}
祝你好运尝试在查询中的“第1部分”前后添加%。像这样:%{$value}%。我再次更正了我的问题。。我已经试过了,但是没有用。@frederickmarcoux你为什么要输入一个MySQL查询?它可以帮助我回答你@Devalshah实际上我已经尝试过像kdaod:kfdlk这样的垃圾值了,quote函数应该足够了。。。如果它不起作用,我也解释不了!是的。我的查询现在显示了。但是结果没有显示。比如$value=kdaod:kfdlk,如果你在MySQL中直接运行该查询,它会返回结果吗?不,它不起作用,因为“$this->getDefaultAdapter->quote%.$value.%`函数在引号中添加了”`。所以like查询与它不匹配。我在你的评论中没有看到额外的引号,您是否可以编辑您的问题以包含完整的、不工作的SQL查询?