我有一个mysql_query
从select *
列表数据库中的'english'
,并且mysql_fetch_assoc
返回一个数组。我尝试使用'flick'
搜索单词in_array()
(实际上存在于数据库中),如果找到了'flick'
,则不应显示它,但会显示它。我认为in_array
函数找不到单词'flick'
。请看下面的代码:
<?php
error_reporting(E_ALL);
require 'db.php';
function spellcheck($word)
{
$output = array();
$word = mysql_real_escape_string($word);
$words = mysql_query("SELECT `word` FROM `english` WHERE LEFT(`word`, 1) = '" .
substr($word, 0, 1) . "'");
while(($words_row = mysql_fetch_assoc($words)) && (in_array($word, $words_row)==false))
{
similar_text($word, $words_row['word'], $percent);
if($percent > 82)
{
$output[] = $words_row['word'];
}
}
return (empty($output)) ? false : $output;
}
if (isset($_GET['word']) && trim($_GET['word']) != null)
{
$word = $_GET['word'];
$spellcheck = spellcheck($word);
if ($spellcheck !== false)
{
echo '<pre>' . print_r($spellcheck, true) . '</pre>';
} else {
echo '<p>' . $word . ' spelled correctly, or no suggestions founds.</p>';
}
}
?>
<form action="" method="GET">
Check single word spelling:
<input type="text" name="word" />
<input type="submit" value="Check" />
</form>
代码返回:
Array (
[0] => flick
[1] => flicks
)
但是应该是:
"spelled correctly, or no suggestions founds."
最佳答案
替换这条线
while(($words_row = mysql_fetch_assoc($words)) && (in_array($word, $words_row)==false))
与
while(($words_row = mysql_fetch_assoc($words))) {
if((in_array($word, $words_row)==false)) {
并在底部关闭if语句