SQL "LIKE" 选择器在我的搜索引擎中的工作方式类似于“=”
SQL "LIKE" selector works like "=" in my search engine
我创建了一个简单的搜索引擎来显示数据库中的结果,但我需要它来显示所有具有相似字符的结果,而不是完整的 word.Soo 例如用户类型 eng-- 它应该return 结果引擎和所有其他带有 eng 的单词,但目前它会 return 只有当你输入整个单词时才会出现一些东西,engine.Guess 我在某处有错误但无法真正找到 it:There 是我的代码。
<?php
$fsearch = "";
if (!empty($_GET['fsearch'])){
$fsearch=$_GET['fsearch'];
$query = "SELECT * FROM food_data_bg WHERE ";
$terms = explode (" ",$fsearch);
$i=0;
foreach($terms as $each){
$i++;
if($i == 1){
$query .= "title LIKE '$each'";
}
else{
$query .= "OR title LIKE '$each'";
}
}
$hostname = "localhost";
$username = "name";
$password = "pass";
$databaseName = "dbName";
$connect = new mysqli($hostname, $username, $password, $databaseName);
$connect->set_charset("utf8");
$query = mysqli_query($connect,$query);
$num_rows = mysqli_num_rows($query);
if($num_rows > 0){
while($row = mysqli_fetch_assoc($query)){
$title = $row["title"];
$fimage = $row["fimage"];
$carbs = $row["carbohydrates"];
$fats = $row["fats"];
$proteins = $row["proteins"];
$CaloriesTotal = $row["calories total"];
echo "
<table id='table1'>
<tbody>
<tr class='Table1-row2'>
<td><a><img src='$fimage'</a></td>
<td>$title</td>
<td>$carbs</td>
<td>$fats</td>
<td>$proteins</td>
<td>$CaloriesTotal</td>
</tr>
</tbody>
</table>";
}
} //got "else" claim here,but i don't think the mistake is in it...
}
?>
感谢任何帮助和建议<3 谢谢!
如果您希望 LIKE 正常运行,则必须包含通配符。因此,如果您要在字符串中查找该词,您可以这样做...
$query .= "title LIKE '%".$each."%'";
百分比 (%) 将匹配任何内容(零个或多个任何类型的字符)。
我创建了一个简单的搜索引擎来显示数据库中的结果,但我需要它来显示所有具有相似字符的结果,而不是完整的 word.Soo 例如用户类型 eng-- 它应该return 结果引擎和所有其他带有 eng 的单词,但目前它会 return 只有当你输入整个单词时才会出现一些东西,engine.Guess 我在某处有错误但无法真正找到 it:There 是我的代码。
<?php
$fsearch = "";
if (!empty($_GET['fsearch'])){
$fsearch=$_GET['fsearch'];
$query = "SELECT * FROM food_data_bg WHERE ";
$terms = explode (" ",$fsearch);
$i=0;
foreach($terms as $each){
$i++;
if($i == 1){
$query .= "title LIKE '$each'";
}
else{
$query .= "OR title LIKE '$each'";
}
}
$hostname = "localhost";
$username = "name";
$password = "pass";
$databaseName = "dbName";
$connect = new mysqli($hostname, $username, $password, $databaseName);
$connect->set_charset("utf8");
$query = mysqli_query($connect,$query);
$num_rows = mysqli_num_rows($query);
if($num_rows > 0){
while($row = mysqli_fetch_assoc($query)){
$title = $row["title"];
$fimage = $row["fimage"];
$carbs = $row["carbohydrates"];
$fats = $row["fats"];
$proteins = $row["proteins"];
$CaloriesTotal = $row["calories total"];
echo "
<table id='table1'>
<tbody>
<tr class='Table1-row2'>
<td><a><img src='$fimage'</a></td>
<td>$title</td>
<td>$carbs</td>
<td>$fats</td>
<td>$proteins</td>
<td>$CaloriesTotal</td>
</tr>
</tbody>
</table>";
}
} //got "else" claim here,but i don't think the mistake is in it...
}
?>
感谢任何帮助和建议<3 谢谢!
如果您希望 LIKE 正常运行,则必须包含通配符。因此,如果您要在字符串中查找该词,您可以这样做...
$query .= "title LIKE '%".$each."%'";
百分比 (%) 将匹配任何内容(零个或多个任何类型的字符)。