简体   繁体   中英

Multiple keywords search

I'm getting trouble to add the search in my site. I cannot figure out the way to make it done.

I 've a table as follow:

TABLE A

id     text
1       Hello there whats up. I'm trying to code.
2       there need to be this code

Now I want search using the Keywords = hello code

And the results should provide me both the rows because both the rows contains some portion of the keyword as follow:

id      text
1       **Hello** there whats up. I'm trying to **code**
2       there need to be this **code**

Also the result should provide the row with max number of keywords matched first.

I tried doing this but it only provide me some of my desire results.

<?php
   $keyword = 'hello code';
   $exloded = explode(' ', $keyword);
   foreach($exploded as value):
      $sth = $db->query("SELECT * FROM A WHERE `text` LIKE :value");
      $sth->execute(array(':value' => '%'.$value.'%'));
      $rows = $sth->fetchAll();
   endforeach;

   echo $rows;
?>

Updated

I simply Did this and it worked fine for me. But I want to know whether this is the correct way to get the work done.

$keyword = hello code;
$query ="SELECT *, MATCH(`page_content`) AGAINST('$keyword' IN BOOLEAN MODE) AS score      FROM super_pages WHERE MATCH(`page_content`) AGAINST('$keyword' IN BOOLEAN     MODE) ORDER BY score DESC";
$sth = $this->db->query($query);
$result = $sth->fetchAll();

$rows will have the data where your keyword code matches in your table you can rewrite your code to match for both keywords as

$keyword = 'hello code';
$exloded = explode(' ', $keyword);
$query = 'SELECT * FROM A ';
$i = 0;
$params = array();
foreach ($exploded as $value):
    if ($i == 0) {
        $query .= ' WHERE `text` LIKE :value_'.$i;
    } else {
        $query .= ' OR `text` LIKE :value_'.$i;
    }
    $params[':value_'.$i] = '%'.$value .'%';
    $i++;
endforeach;
$sth = $db->query($query);
$sth->execute($params);
$rows = $sth->fetchAll();
echo '<pre>';print_r($rows);echo '</pre>';

Build your query in loop(over your provided keywords) and assign unique placeholders in query to match for all values

Edit for full text search

Using full text search you can match exact same phrase with provided keyword,In order to work with full text search you need an index of type FULLTEXT .

ALTER TABLE `A` ADD FULLTEXT INDEX `fulltextindex` (`text`); 

And query will be like

$keyword = 'hello code';
$exloded = explode(' ', $keyword);
$where = '';
$i = 0;
$select = array();
$params = array();

foreach ($exploded as $value):
    $select[]= ' MATCH(`text`) AGAINST(:value_'.$i.' IN BOOLEAN MODE) ';
    if ($i == 0) {
        $where  .= ' WHERE MATCH(`text`) AGAINST(:value_'.$i.' IN BOOLEAN MODE)';
    } else {
        $where  .= ' OR MATCH(`text`) AGAINST(:value_'.$i.' IN BOOLEAN MODE)';
    }
    $params[':value_'.$i] =  $value ;
    $i++;
endforeach;

$query ='SELECT *,'. implode( ' + ',$select).' AS score FROM A '.$where.' ORDER BY score DESC';
$sth = $db->query($query);
$sth->execute($params);
$rows = $sth->fetchAll();
echo '<pre>';print_r($rows);echo '</pre>';

Above code will produce a query like

SELECT *,
MATCH(`text`) AGAINST('hello' IN BOOLEAN MODE)
+ 
MATCH(`text`) AGAINST('code' IN BOOLEAN MODE) AS score
FROM A 
WHERE MATCH(`text`) AGAINST('hello' IN BOOLEAN MODE)
OR  MATCH(`text`) AGAINST('code' IN BOOLEAN MODE)
ORDER BY score DESC

Alias score in above query will have value for each row and its matched score thus you can order your result in descending manner to show the records first which has a highest score.

Note: You can use Full text search in Myisam but for innodb you have to upgrade your Mysql to 5.6 which supports full text searching in innodb too

you can use the following code:

<?php
   $keyword = 'hello code';
   $exloded = explode(' ', $keyword);

   $sql = "SELECT * FROM A WHERE ";


   foreach($exploded as $value):
      $sql .= "text LIKE '" . $value "%' OR ";
   endforeach;

   // remove last 'OR '
   $sql = substr($sql, -3);

   $result = mysqli_query($con, $sql);


   //................
 ?>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM