2012-07-04 26 views
0

我試圖讓這個代碼工作,併爲我的生活不能得到它...我想要一個搜索,顯示你的意思。與代碼我有我得到它「你的意思是:陣列1:6」我在這裏有什麼問題?mySQL顯示爲數組

$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']; 
+0

['var_dump($ bestMatch ['word']);'](http://php.net/manual/en/function.var-dump.php)會告訴你你做錯了。 – PeeHaa

+1

請不要將'mysql_ *'函數用於新代碼。他們不再被維護,社區已經開始[棄用流程](http://goo.gl/KJveJ)。請參閱[**紅框**](http://goo.gl/GPmFd)?相反,您應該瞭解[準備好的語句](http://goo.gl/vn8zQ)並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli的)。如果你不能決定,[本文](http://goo.gl/3gqF9)將有助於選擇。如果你關心學習,[這裏是很好的PDO教程](http://goo.gl/vFWnC)。 –

回答

4

你通過你的整個搜索結果集爲levenshtein()函數,而不是關鍵字:

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; 
    } 
} 
+0

這樣做了...現在真正快速你能解釋什麼1,2,1引用這裏...我認爲它抓住最接近的匹配。但每個代表什麼? –

+0

他們對您所做的事情沒有必要,所以您可以安全地將其刪除。 –

+0

男人必須有一個更好的方式來做到這一點...當我搜索調度它說你是否意味着持續,當它應該說你的意思是安排或類似 –

0
<?php 
while ($keyword = mysql_fetch_array($result)) 
?> 

$關鍵字是一個數組!你的字是在$關鍵字[0]或$關鍵字['關鍵字']。 與

$bestMatch = array('word' => $keyword['keyword'], 'match' => $lev); 
0

由於PeeHaajeroen取代你的7號線已經說過,MySQL的結果返回一個數組。

放棄吧,看看裏面有什麼了

if ($bestMatch['match'] > 0) { 
    var_dump($bestMatch['word']); 
    echo 'Did you mean: <strong>'.$bestMatch['word'].'</strong> l:'.$bestMatch['match']; 
} 

很可能是另一個[]在那裏,所以這可能工作:

echo 'Did you mean: <strong>'.$bestMatch['word'][0].'</strong> l:'.$bestMatch['match']; 
1

$關鍵字是一個數組(一維) ,而不是一個專欄。你應該mysql_fetch_field你想要的列