0

I am running MySQL version 5.1.57

I have a HTML form where a user can insert a search-string. I create a $_SESSION on this string, then I run it in a MySQLquery. 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(?). How can I be able to search for multiple words using the environment that I have?

David
  • 1,171
  • 8
  • 26
  • 48

1 Answers1

0

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";

With that you search first for the whole string "word1 word2" and after you search in you database for "word1" and "word2". With this solution you should handle a word ignore list, because words like "a, an, the, or, ..." shouldn't be seek ...

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.

PoulsQ
  • 1,936
  • 1
  • 15
  • 22