input
input

Reputation: 7519

Autocomplete MySQL ssue

I am using this autocomplete tutorial to perform an autocomplete search. The problem is in SQL it displays autocomplete for more than one word only if the words are in sequence.

Example: The quick brown fox jumped over the lazy dog.

If the search is performed for quick, it will display all results containing quick. But if I perform a search for quick lazy, it doesn't display anything. On the other hand, if I perform a search for quick brown, it will display the above sentence.

How can I make the search autocomplete for more than one word in any random order?

This is the SQL:

  $sRequest = "SELECT * FROM `table` WHERE `columnname` LIKE '%{$sParam}%' ORDER BY `columnname`";
        $aItemInfo = $GLOBALS['MySQL']->getAll($sRequest);
        foreach ($aItemInfo as $aValues) {
            echo $aValues['columnname'] . "\n";
        }

Upvotes: 0

Views: 195

Answers (1)

Marco
Marco

Reputation: 57573

You should split your array of words to search and build a query like this:

SELECT * FROM `table` 
WHERE `columnname` LIKE '%{$sParam1}%' 
  AND `columnname` LIKE '%{$sParam2}%'
  AND `columnname` LIKE '%{$sParam3}%'
ORDER BY `columnname`";

Upvotes: 1

Related Questions