Search code examples
phpmysqlarrayslevenshtein-distance

mySQL showing as array


Im trying to get this code to work and for the life of me can not get it going... I want a search that shows a Did you mean. with the code i have all i get it "Did you mean: Array l:6" what is wrong with what i have here?

$my_word = $_REQUEST['value'];
$bestMatch = array('word' => $my_word, 'match' => 2);
$result = mysql_query("SELECT keyword FROM athena");
while ($keyword = mysql_fetch_array($result)) {
    $lev = levenshtein ($keyword, $my_word, 1, 2, 1);
    if (!isset($lowest) || $lev < $lowest) {
       $bestMatch = array('word' => $keyword, 'match' => $lev);
       $lowest = $lev;
     }
 }
 if ($bestMatch['match'] > 0)
   echo 'Did you mean: <strong>'.$bestMatch['word'].'</strong> l:'.$bestMatch['match'];

Solution

  • Your passing your entire search result set to the levenshtein() function instead of the keyword:

    while ($row= mysql_fetch_array($result)) {
        $lev = levenshtein ($row['keyword'], $my_word, 1, 2, 1);
        if (!isset($lowest) || $lev < $lowest) {
           $bestMatch = array('word' => $row['keyword'], 'match' => $lev);
           $lowest = $lev;
        }
    }