带有预准备语句的全文搜索返回空数组

时间:2019-02-09 18:08:20

标签: php mysql full-text-search prepared-statement

我正在尝试在我的应用程序中实现MySQL全文搜索。

但是只有当我使用一条准备好的语句时,我才能得到一个空数组。如果我只是将php变量放入SQL查询中,就可以正常工作,但这不是我想要的。

这将返回应有的几条记录:

try { $sql = /** @lang mysql */ "SELECT *, MATCH (first_name, last_name, email, username) AGAINST (('$string*') IN BOOLEAN MODE) AS score FROM customer p WHERE MATCH (first_name, last_name, email, username) AGAINST (('$string*') IN BOOLEAN MODE) ORDER BY score DESC LIMIT 100; "; $stmt = $conn->prepare($sql); $stmt->execute(); }catch (\Exception $e){

以下代码返回[]:

try { $sql = /** @lang mysql */ "SELECT *, MATCH (first_name, last_name, email, username) AGAINST ((':string*') IN BOOLEAN MODE) AS score FROM customer p WHERE MATCH (first_name, last_name, email, username) AGAINST ((':string*') IN BOOLEAN MODE) ORDER BY score DESC LIMIT 100; "; $stmt = $conn->prepare($sql); $stmt->execute(['string' => $string]);

查询如下:

SELECT *, MATCH (first_name, last_name, email, username) AGAINST ((:string*) IN BOOLEAN MODE) AS score FROM customer p WHERE MATCH (first_name, last_name, email, username) AGAINST ((:string*) IN BOOLEAN MODE) ORDER BY score DESC LIMIT 100;

导致以下错误:

  

SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有一个错误;请参见语法。检查与您的MySQL服务器版本相对应的手册以获取在')IN BOOLEAN MODE)AS评分附近使用的正确语法   来自客户p   WHE'在第2行

我希望第二个查询与第一个查询相同。

据我所知,存在一些我不知道和/或找不到的规则的“技巧”。请告诉我:

如何对已准备好的语句使用全文搜索。语法是什么?

0 个答案:

没有答案