如何通过php和mysql进行更智能的搜索

我有一个数据库上的地方表,它包含两行标题和描述

我通过PHP和MYSQL进行了搜索

$select_place = $mysqli->query("SELECT * FROM places where title LIKE '%$searchname%' or description LIKE '%$searchname%'");
$num_place = $select_place->num_rows;

while ($rows_place = $select_place->fetch_array(MYSQL_ASSOC)){

$id_place            = $rows_place ['id'];
$title_place         = $rows_place ['title'];
$description_place   = $rows_place ['description'];

echo "<p>{$title_place}</p><br>";

}

它运作良好,但是,例如,如果你搜索单词塔如果以错误的方式写,如twer或towr它不起作用我怎样才能使它更聪明?

最佳答案 一个不错的选择是包含MySQL SOUNDEX()函数:

SELECT
    *
FROM
    places
WHERE
    title LIKE '%$searchname%'
    OR description LIKE '%$searchname%'
    OR SOUNDEX(title) = SOUNDEX('$searchname')
    OR SOUNDEX(description) = SOUNDEX('$searchname')

这将匹配你的例子中的towr和twer.

更多信息可以在这里找到:

http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_soundex

注意:

This function, as currently implemented, is intended to work well with
strings that are in the English language only. Strings in other
languages may not produce reliable results.

点赞