我在数据库中有一个位置表,它包含两行标题和描述
我是这样用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)搜索单词 tower,它就不起作用 我怎样才能让它更智能?
最佳答案
一个不错的选择是包含 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.
关于php - 如何通过 php 和 mysql 进行更智能的搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35957537/