2013-03-11 45 views
0

我想對mysql查詢結果執行levenshtein。PHP Levenshtein查詢結果

查詢看起來是這樣的:

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

在這裏,我準備在萊文斯坦操作一切:

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

而這僅僅是從手動的萊文斯坦操作:

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

感謝Jaitsu我擺脫了錯誤/警告消息,但levenshtein現在扔荷蘭國際集團一個意想不到的結果:

無論輸入的是,它永遠不會找到匹配的結果,並且最接近的匹配將始終= ^h

例子:

輸入字:戰地3回到Karkand你的意思是:H?
輸入單詞: 星際爭霸2自由之翼你的意思是:H?

說實話,我沒有什麼線索#回事現在...

+0

是什麼'$ words'?這不是一個數組,這就是你的問題 – JamesHalsall 2013-03-11 12:52:33

+0

那麼我如何使它成爲一個數組呢?我想要它存儲我的查詢結果...手冊聲明一個數組是這樣的:'$ words = array('apple','pineapple','banana','orange', '蘿蔔','胡蘿蔔','豌豆','豆','馬鈴薯');'但是我怎麼用我的查詢結果聲明一個數組? – SubZero 2013-03-11 12:55:11

+0

請參閱下面的答案 – JamesHalsall 2013-03-11 12:58:14

回答

1

你從你的數據庫中獲取遊戲(的話)的邏輯是正確的,但你需要刪除...

$words = $row_GID['game'];

,並通過在$row_GID變量設置爲循環。

foreach循環

則...

foreach ($row_GID as $row) { 
    $word = $row['game']; 
    //proceed as normal 
} 
+0

感謝您提供解決方案!現在它的工作沒有拋出任何錯誤,但我得到的結果是相當......意想不到的。例如,如果輸入是:**上古卷軸Skyrim ** levenshtein問我是否意味着H ......但是在我的數據庫中沒有名爲** H **的名稱,所以我猜這個名字的第一個字母是Halo這也是意想不到的... – SubZero 2013-03-11 13:03:24