我有一个MySQL查询如下;搜索建议 - PHP - MySQL
$query = "SELECT * FROM dictionary WHERE word LIKE '%".$word."%' ORDER BY word LIMIT 10";
这将搜索(并显示)我的字典db中的单词。
该搜索将返回;
Drunken for Drunk, etc, etc..
, Drunken for Drunke, etc, etc..
和Drunken for Drunken, etc, etc..
但它不会为Drunkin
返回Drunken
。我想将这个单词显示为一个建议词(就像我们在google中看到的那样)。我怎样才能做到这一点?
下面是我的完整代码供参考;
$db = new pdo("mysql:host=localhost;dbname=dictionary", "root", "password");
$query = "SELECT * FROM dictionary WHERE word LIKE '%".$word."%' ORDER BY word LIMIT 10";
$result = $db->query($query);
$end_result = '';
if ($result) {
while ($r = $result->fetch(PDO::FETCH_ASSOC)) {
$end_result .= $r['word'].'<br>';
}
}
echo $end_result;
一旦它到达i字符,它就不再匹配。您可以在查询中使用子字符串以使查询更通用。 substr('$ word,0,5)会给你醉,这会让你在你的结果集中醉酒。 – chapman84