Using Match and Against in MySQL and CodeIgniter

北慕城南 提交于 2019-12-11 01:52:46

问题


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%' OR prod_sku LIKE '%silk%' OR `prod' at line 3

SELECT prod_id FROM (product_search) WHERE MATCH (prod_title, prod_desc) AGAINST ('silk') OR prod_id LIKE '%silk%' OR prod_sku LIKE '%silk%' OR prod_title LIKE '%silk%' OR prod_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') OR prod_id LIKE '%silk%' OR prod_sku LIKE '%silk%' OR prod_title LIKE '%silk%' OR prod_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

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