我使用這種方式搜索地點。我如何在PHP和MYSQL中通過相關性進行搜索排序
$searchname = stringsafe($_GET['search']);
$searchname2 = explode(" ", $searchname);
$query = "SELECT * FROM places WHERE ";
foreach ($searchname2 as $each) {
$i++;
if($i == 1){
$query .= " title LIKE '%$each%'
OR description LIKE '%$each%'
OR SOUNDEX(title) = SOUNDEX('$each')
OR SOUNDEX(description) = SOUNDEX('$each')";
}else{
$query .= " OR title LIKE '%$each%'
OR description LIKE '%$each%'
OR SOUNDEX(title) = SOUNDEX('$each')
OR SOUNDEX(description) = SOUNDEX('$each')";
}
}
$select_place = $mysqli->query("$query limit 20");
$num_place = $select_place->num_rows;
if($num_place == 0){
echo "<div class='message-no'>No results</div>";
}
else{
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 $title_place."<br>";
}
它運作良好,但結果沒有以正確的方式排序。這是我試過的:
$select_place = $mysqli->query("$query ORDER BY relevance DESC limit 20");
我沒有達到預期的結果,它應該按相關性排序。
什麼是錯誤? –
首先'MYSQL_ASSOC'應該是'MYSQLI_ASSOC' –
你怎麼在這裏定義「相關性」? –