我发现布尔全文搜索非常有用,但是由于某些原因,我无法弄清楚为什么我无法从查询中得到结果。
我的查询如下:
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 "<br>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;
}
然后搜索字段:
<input type="text" name="fname" id="fname" class="form-control" value="" placeholder="Search By Name">
但是,当我搜索时,无论是单独fname
还是fname+lname
进行搜索,都没有结果。
答案 0 :(得分:3)
您的问题出在表达式的这一部分:
(+$_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']}"
。