2013-03-11 78 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