MySQLi Full Text Search For Multiple Column Category

穿精又带淫゛_ 提交于 2019-12-10 19:46:47

问题


I want to make mysqli full text search with or without multiple category based on checkbox selection.

Search is based on four category within same column.

Search category are windows, games, tablet, mobile.

When category/multiple category selected through checkbox selection search should be done for selected category only.

Table Structure

id  category   title                date ...
1   windows    windows7             d1  ...
2   games      windows games        d2  ...
3   tablet     windows tablet       d3  ...
4   mobile     windows mobile       d4  ...
5   windows    windows8             d5  ...
6   windows    windows vista        d6  ...

checkboxes for search are

Search in

  <li><label><input name="all" type="checkbox" checked="checked" />All</label></li>
  <li><label><input name="windows" type="checkbox" />Windows OS</label></li>
  <li><label><input name="mobile" type="checkbox" />Windows Mobile</label></li>
  <li><label><input name="games" type="checkbox" />Windows Games</label></li>
  <li><label><input name="tablet" type="checkbox" />Windows Tablet</label></li>

Example1:

Search for 'windows' should return result as

windows7
windows8 
windows vista

When only checkbox windows is checked

Example2:

Search for 'windows' should return result as

windows7
windows8 
windows vista
windows games

When checkbox windows and games are checked.    

I have made a query but it is not working at all.

$query = "SELECT * FROM $table WHERE ";
$query .= "category='' ";
$query .= "OR category='windows' ";
$query .= "OR category='games' ";
$query .= "OR category='mobile' ";
$query .= "OR category='tablet' ";

$query .= " AND MATCH (title) AGAINST ('+$keyword*' IN BOOLEAN MODE) ORDER by id desc, title desc LIMIT 10";

I have also tried like this but not working.

$query = "SELECT * FROM $table WHERE ";
$query .= "MATCH (category) AGAINST ('' IN BOOLEAN MODE) ";
$query .= "OR MATCH (category) AGAINST ('windows' IN BOOLEAN MODE) ";
$query .= "OR MATCH (category) AGAINST ('games' IN BOOLEAN MODE) ";
$query .= "OR MATCH (category) AGAINST ('mobile' IN BOOLEAN MODE) ";
$query .= "OR MATCH (category) AGAINST ('tablet' IN BOOLEAN MODE) ";

$query .= " AND MATCH (title) AGAINST ('+$keyword*' IN BOOLEAN MODE) ORDER by id desc, title desc LIMIT 10";

Please see why its not working and suggest how search query can be made for no of fields selected.

Thanks.


回答1:


SELECT * FROM table WHERE MATCH (field1, field2, field3, field4) 
                          AGAINST ('keyword' IN BOOLEAN MODE)

seems what you need.

But your query is wide open to SQL injection.

Now it's possible to answer. so, here goes a solution based on safeMysql (as raw mysqli will take too much code)

$sql  = "SELECT * FROM ?n WHERE category IN(?a) 
          AND MATCH (title) AGAINST (?s IN BOOLEAN MODE) ORDER by id desc LIMIT 10";
$data = $db->($sql,$table,$_GET['category'], $_GET['keyword']);

Note that you have to call your checkbox fields as

<input name="category[]" value="windows" type="checkbox" />


来源:https://stackoverflow.com/questions/17294924/mysqli-full-text-search-for-multiple-column-category

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!