0

I want to perform a levenshtein on a mysql query result.

The query looks like this:

$query_GID = "select `ID`,`game` from `gkn_catalog`";
$result_GID = $dbc->query($query_GID);
$row_GID = mysqli_fetch_array($result_GID,MYSQLI_ASSOC);

And here I prepare everything for the levenshtein operation:

$shortest = -1;
$input = $game_title;

And this is just the levenshtein operation from the manual:

   foreach ($row_GID as $row) {
$word = $row['game'];

// calculate the distance between the input word,
// and the current word
$lev = levenshtein($input, $word);

// check for an exact match
if ($lev == 0) {
// closest word is this one (exact match)
$closest = $word;
$shortest = 0;

// break out of the loop; we've found an exact match
break;
}
// if this distance is less than the next found shortest
// distance, OR if a next shortest word has not yet been found
if ($lev <= $shortest || $shortest < 0) {
// set the closest match, and shortest distance
$closest  = $word;
$shortest = $lev;
}
}
echo "Input word: $input\n";
if ($shortest == 0) {
echo "Exact match found: $closest\n";
} else {
echo "Did you mean: $closest?\n";
}

Thanks to Jaitsu I got rid of the error/warning message, however the levenshtein is now throwing an unexpected result:

Whatever the input is, it won't ever find a matching result and the closest match will always be = H

Examples:

Input word: Battlefield 3 Back to Karkand Did you mean: H?
Input word: Starcraft 2 Wings of Liberty Did you mean: H?

To be honest, I don't have a clue what#s going on right now...

4

1 回答 1

1

Your logic for fetching the games (words) from your database is correct, but you need to remove...

$words = $row_GID['game'];

and pass in the $row_GID variable to your loop.

Then in your foreach loop...

foreach ($row_GID as $row) {
    $word = $row['game'];
    //proceed as normal
}
于 2013-03-11T12:57:56.267 回答