Symfony / MySQL:特殊字符的全文搜索语法错误

时间:2016-12-29 12:03:13

标签: mysql sql symfony full-text-search

我的全文搜索查询存在问题。它是从Symfony(2.8)控制器内部发送的,如果这很重要的话。代码是:

// not processed text (apart from multiple spaces) from form:
$searchTerm = preg_replace('!\s+!', ' ', trim($data['query'])); 
// for full-text search:
$searchTermPhrase = str_replace(' ', '* ', $searchTerm) . '*';

$entityManager = $this->getDoctrine()->getManager();
// as far as I remember, query taken from StackOverflow after some tests
$statement = $entityManager->getConnection()
                           ->prepare(
                             "SELECT COUNT(id) AS books_count
                              FROM book
                              WHERE
                              MATCH(title) AGAINST (:searchTermPhrase IN BOOLEAN MODE) > 5 
                              AND active = true
                              ");
$statement->execute(array('searchTermPhrase' => $searchTermPhrase));

$titlesCount = $statement->fetch();
$titlesCount = $titlesCount['books_count'];

现在,它很棒。大多数时候。但是如果用户在内部添加了一些特殊字符,例如(title)"title"等,则会失败:

SQLSTATE[42000]: Syntax error or access violation: 
1064 syntax error, unexpected $end, expecting 
FTS_TERM or FTS_NUMB or '*'

我不明白,我使用参数化查询,它应该自动处理这种情况......?我不想最终剥离所有非字母数字字符或测试,这确实会导致问题,但我没有想法。

1 个答案:

答案 0 :(得分:0)

看起来这是InnoDB问题,请参阅此报告https://bugs.mysql.com/bug.php?id=72605。我遇到了同样的问题,将引擎更改为MyISAM它开始工作