I have found Boolean full-text search to be much helpful but for some reason, I have failed to figure out why I am not getting results from my query.
My query is as follows:
if(isset($_REQUEST['fname']) and $_REQUEST['fname']!=""){
$condition .= ' AND MATCH (fname, lname, mname) AGAINST('.(+$_REQUEST['fname']).' IN BOOLEAN MODE) ' ;
}
Which is backed up by this function;
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;
}
Then the search field:
<input type="text" name="fname" id="fname" class="form-control" value="" placeholder="Search By Name">
But when I search, I get no results, whether I search by fname
alone or fname+lname
.
Your problem is in this part of the expression:
(+$_REQUEST['fname'])
When you add the +
in front of the value, PHP tries to convert it into a numeric value and (since it is presumably a word) gives a non-numeric value warning and returns 0. I think you meant to put the +
into the overall expression ie
$condition .= " AND MATCH (fname, lname, mname) AGAINST('+{$_REQUEST['fname']}' IN BOOLEAN MODE) ";
A prepared statement would have helped you avoid these issues, writing
$condition .= " AND MATCH (fname, lname, mname) AGAINST(? IN BOOLEAN MODE) ";
and then binding the parameter to "+{$_REQUEST['fname']}"
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.