简体   繁体   English

MySQL(innoDB)PHP搜索多个单词

[英]MySQL (innoDB) PHP search multiple words

I am running MySQL version 5.1.57 我正在运行MySQL版本5.1.57

I have a HTML form where a user can insert a search-string. 我有一个HTML表单,用户可以在其中插入搜索字符串。 I create a $_SESSION on this string, then I run it in a MySQL query. 我在此字符串上创建了$_SESSION ,然后在MySQL查询中运行它。 Something like this: 像这样:

<?php

  $sql = mysql_query ("SELECT 
                        s.student_firstname, s.student_lastname 
                      FROM students s 
                      WHERE (
                             s.student_firstname LIKE '%$searchstring%' OR
                             s.student_lastname LIKE '%$searchstring%
                             )
                      AND s.isActive = '1' "); 
?>

The problem is when a user is searching for multiple words. 问题是当用户正在搜索多个单词时。 Then my query fails because it is trying to match the string against the values in either column. 然后我的查询失败,因为它试图将字符串与任一列中的值进行匹配。

I've read something about MySQL FULLTEXT indexing but as far as I understand, it only works on MyISAM tables(?). 我已经阅读了有关MySQL FULLTEXT索引的内容,但据我了解,它仅适用于MyISAM表(?)。 How can I be able to search for multiple words using the environment that I have? 如何使用所拥有的环境搜索多个单词?

I think you should split your searched string on space (" ") and insert each segment in your query, or in another query. 我认为您应该在空间(“”)上分割搜索到的字符串,然后将每个段插入查询或其他查询中。 For example : 例如 :

$str = "word1 word2"; $ str =“ word1 word2”;

With that you search first for the whole string "word1 word2" and after you search in you database for "word1" and "word2". 这样,您首先搜索整个字符串“ word1 word2”,然后在数据库中搜索“ word1”和“ word2”。 With this solution you should handle a word ignore list, because words like "a, an, the, or, ..." shouldn't be seek ... 使用此解决方案,您应该处理单词忽略列表,因为不应查找诸如“ a,an,the或...”之类的单词。

I'm not sure there is an other way with an innoDB table ... The best solution is obviously to use the "match against" command, but it's only available with a full text index under MyISAM. 我不确定innoDB表是否有其他方法。最好的解决方案显然是使用“匹配对象”命令,但是仅在MyISAM下具有全文本索引时才可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM