我发现布尔全文本搜索非常有用,但是由于某些原因,我无法弄清楚为什么我无法从查询中得到结果。
我的查询如下:
if(isset($_REQUEST['fname']) and $_REQUEST['fname']!=""){ $condition .= ' AND MATCH (fname, lname, mname) AGAINST('.(+$_REQUEST['fname']).' IN BOOLEAN MODE) ' ; } 该功能支持哪个功能;
public function get($tableName, $whereAnd = array(), $whereOr = array(), $whereLike = array()) { $cond = ''; $s=1; $params = array(); foreach($whereAnd as $key => $val) { $cond .= " And ".$key." = :a".$s; $params['a'.$s] = $val; $s++; } foreach($whereOr as $key => $val) { $cond .= " OR ".$key." = :a".$s; $params['a'.$s] = $val; $s++; } foreach($whereLike as $key => $val) { $cond .= " OR ".$key." like '% :a".$s."%'"; $params['a'.$s] = $val; $s++; } $stmt = $this->pdo->prepare("SELECT $tableName.* FROM $tableName WHERE 1 ".$cond); try { $stmt->execute($params); $res = $stmt->fetchAll();
if (! $res || count($res) != 1) {
return $res;
}
return $res;
} catch (\PDOException $e) {
throw new \RuntimeException("[".$e->getCode()."] : ". $e->getMessage());
}
}
public function getAllRecords($tableName, $fields='*', $cond='', $orderBy='', $limit='') { //echo "SELECT $tableName.$fields FROM $tableName WHERE 1 ".$cond." ".$orderBy." ".$limit; //print "
SELECT $fields FROM $tableName WHERE 1 ".$cond." ".$orderBy." ".$limit; $stmt = $this->pdo->prepare("SELECT $fields FROM $tableName WHERE 1 ".$cond." ".$orderBy." ".$limit); //print "SELECT $fields FROM $tableName WHERE 1 ".$cond." ".$orderBy." " ; $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); return $rows; } 然后是搜索字段:
但是当我进行搜索时,无论是fname一个人还是一个人,都没有结果fname+lname。
问题来源于stack overflow
您的问题在于表达式的这一部分:
(+$_REQUEST['fname']) 当您+在值前面添加时,PHP会尝试将其转换为数字值,并且(因为它大概是一个单词)会给出非数字值警告并返回0。我想您打算将其+放入整体表达式中即
$condition .= " AND MATCH (fname, lname, mname) AGAINST('+{$_REQUEST['fname']}' IN BOOLEAN MODE) "; 准备一份声明会帮助您避免这些问题,
$condition .= " AND MATCH (fname, lname, mname) AGAINST(? IN BOOLEAN MODE) "; 然后将参数绑定到"+{$_REQUEST['fname']}"。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。