I have a mysql_query to select * from an 'english' list database and mysql_fetch_assoc returns an array. I try to search word 'flick' (which actually exists in the database) by using in_array() if 'flick' is found, it shouldn't be shown but it is shown. I think in_array function does not find the word 'flick'. Please look at code below:
<?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>
The code returns:
Array (
[0] => flick
[1] => flicks
)
But it should be:
"spelled correctly, or no suggestions founds."
mysql_*functions anymore. They're deprecated for a long time and get finally removed with PHP7 in the next few months. Use PDO or mysqli instead.mysql_*is the reason of the wrong answer?mysqli_*and sorrily the answer is the same. It means there is no deference between mysl and mysqli in my localhost xampp.mysqliwill work in the future, andmysql_queryis already producing tons of warnings in PHP 5.5.