问题
I am trying to implement a search feature into my codeIgniter project. I have a table called product_search, where I am set a fulltext index using the following command:
ALTER TABLE `product_search` ADD FULLTEXT (`prod_title`, `prod_desc`)
I then search it using the following codeIgniter code:
$this->db->select("prod_id");
$this->db->where("MATCH (`prod_title`, `prod_desc`) AGAINST ('{$searchStr}')");
$this->db->or_where("prod_id LIKE '%{$searchStr}%'");
$this->db->or_where("prod_sku LIKE '%{$searchStr}%'");
$this->db->or_where("prod_title LIKE '%{$searchStr}%'");
$this->db->or_where("prod_desc LIKE '%{$searchStr}%'");
$query = $this->db->get("product_search");
This, however, results in the following error:
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AGAINST ('silk') OR
prod_id
LIKE '%silk%' ORprod_sku
LIKE '%silk%' OR `prod' at line 3SELECT
prod_id
FROM (product_search
) WHEREMATCH
(prod_title
,prod_desc
) AGAINST ('silk') ORprod_id
LIKE '%silk%' ORprod_sku
LIKE '%silk%' ORprod_title
LIKE '%silk%' ORprod_desc
LIKE '%silk%'Filename: models/products_model.php
Line Number: 430
Why is it putting backticks around the MATCH?
If I remove the space inbetween MATCH and the brackets following it, then I get this error message:
Error Number: 1191
Can't find FULLTEXT index matching the column list
SELECT
prod_id
FROM (product_search
) WHERE MATCH(prod_title
,prod_desc
) AGAINST ('silk') ORprod_id
LIKE '%silk%' ORprod_sku
LIKE '%silk%' ORprod_title
LIKE '%silk%' ORprod_desc
LIKE '%silk%'Filename: models/products_model.php
Line Number: 430
However, I used the top command to add a fulltext index. What is going wrong?
回答1:
I solved the problem by adding IN BOOLEAN MODE to against section the statement as below:
SELECT `prod_id`
FROM (`product_search`)
WHERE MATCH (`prod_title`, `prod_desc`) AGAINST ('silk' IN BOOLEAN MODE)
OR `prod_id` LIKE '%silk%' OR `prod_sku` LIKE '%silk%'
OR `prod_title` LIKE '%silk%'
OR `prod_desc` LIKE '%silk%';
来源:https://stackoverflow.com/questions/11762051/using-match-and-against-in-mysql-and-codeigniter