mySQL 显示为数组


mySQL showing as array

我试图让这段代码工作,而我的生活却无法让它运转......我想要一个显示"你的意思是"的搜索。 有了我得到的所有代码"你的意思是:数组 l:6"我这里有什么问题?

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

将整个搜索结果集传递给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;
    }
}

$keyword是一个数组(一维),而不是单个列。您应该mysql_fetch_field所需的列

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

$keyword是一个数组!您的单词在$keyword[0]或$keyword["关键字"]中。将第 7 行替换为

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

正如PeeHaa和jeroen已经说过的,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'];