【发布时间】:2012-06-01 12:02:45
【问题描述】:
我有一个如下的 MySQL 查询;
$query = "SELECT * FROM dictionary WHERE word LIKE '%".$word."%' ORDER BY word LIMIT 10";
这将在我的字典数据库中搜索(并显示)单词。
搜索将返回;
Drunken for Drunk, etc, etc.. ,
Drunken for Drunke, etc, etc..
和Drunken for Drunken, etc, etc..
但它不会为 Drunkin 返回 Drunken。我想将这个词显示为建议词(就像我们在谷歌中看到的那样)。我该怎么做?
以下是我的完整代码供参考;
$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) 会给你 Drunk 这会让你 Drunkin 在你的结果集中。
标签: php mysql search pdo search-suggestion