qwerty
qwerty

Reputation: 5246

PHP/Codeigniter - MySQL error on fulltext search?

I'm trying to perform a fulltext search with codeigniters built in Active Record methods. I had a "custom" query string before that i pretty much copied into codeigniter code (so to speak). Somewhere in the where statement there's an error, but i can't find it.

$this->db->select('movies.*');
$this->db->from('movies');
$this->db->join('movies_genres', 'movies.id = movies_genres.movie_id', 'left');
$this->db->join('genres', 'movies_genres.genre_id = genres.id', 'left');
$this->db->where('MATCH (movies.movie_title) AGAINST (`'.$query.'`) IN BOOLEAN MODE', null, false);
//$this->db->group_by('movies.id');
//$this->db->limit($count, $page);

By default codeigniter helps out by adding backticks around variables to make them more secure. I tried turning that off as well by setting the last param to false, but still no luck. By commenting out the last two lines i still got the same error, and if i comment out the where statement, the error disappears (and i get results).

Here is the query generated by Codeigniter:

SELECT `movies`.* FROM (`movies`) LEFT JOIN `movies_genres` ON `movies`.`id` = `movies_genres`.`movie_id` LEFT JOIN `genres` ON `movies_genres`.`genre_id` = `genres`.`id` WHERE MATCH (movies.movie_title) AGAINST (`the`) IN BOOLEAN MODE

And here is the error:

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 'BOOLEAN MODE' at line 5

Any help is greatly appreciated!

Upvotes: 0

Views: 1457

Answers (2)

qwerty
qwerty

Reputation: 5246

I accidentally put the last ending parenthesis in the wrong place. It should be after "BOOLEAN MODE", not after the search string where i put it.

$this->db->where("MATCH (movies.movie_title) AGAINST ('+".$query."' IN BOOLEAN MODE)", null, false);

Upvotes: 1

Nanne
Nanne

Reputation: 64409

This

 AGAINST (`'.$query.'`)

shouldn't be with backticks, but with quotes. try

AGAINST (\''.$query.'\')

also, the syntax seems to be

AGAINST ('+MySQL -YourSQL' IN BOOLEAN MODE);

if I read http://dev.mysql.com/doc/refman//5.5/en/fulltext-boolean.html correctly

Upvotes: 1

Related Questions