我有一个 SQL 虚拟主机数据库,其中包含一个描述为“描述”的表。现在数据库与我的应用程序完全正常,但我似乎有一个关于搜索中多个关键字的问题。截至目前,搜索只会找到在我的数据库中“描述”表内的搜索框中输入的第一个单词。假设我的用户通过应用程序中的搜索框输入“Dragon Tattoo”,它将仅显示“Dragon”的所有内容。总之,让我的搜索框列出任何包含多个带空格的单词的关键字的最佳方法是什么精确匹配。
这是我用于搜索功能的服务器端 PHP:
function _search($text){
$dbObj=$this->CONN;
$query="SELECT * FROM tatoo_user_info where description LIKE '%$text%' OR name LIKE '%$text%' ORDER BY create_date DESC";
$stmt=$dbObj->prepare($query);
$res=$stmt->execute();
$rows=$stmt->fetchAll(PDO::FETCH_ASSOC);
$resultant=array();
foreach($rows as $values){
$array=array();
$array=$values;
$query1="SELECT * FROM images where tui_id=:id";
$stmt1=$dbObj->prepare($query1);
$stmt1->bindParam(':id',$values['id']);
$res=$stmt1->execute();
$row=$stmt1->fetchAll(PDO::FETCH_ASSOC);
$arr_image=array();
foreach($row as $images){
$arr_image[]=$images['name'];
}
$resultant[]=array_merge($array,array("images"=>$arr_image));
}
//print_r($resultant);die;
if(count($resultant)>=1){
$result_a=array("status"=>"SUCCESS","message"=>"Successfully Fetched","data"=>$resultant);
echo json_encode($result_a);
}
else{
$result_a=array("status"=>"FAILURE","message"=>"Fail to find");
echo json_encode($result_a);
}
}
最佳答案
我不确定这个的性能,但你可以尝试多个查询:
$str = "Dragon Tattoo and much more";
$arr_words = explode(" ", $str);
foreach($arr_words as $word)
{
$word = trim($word);
$sql="SELECT * FROM tatoo_user_info where description LIKE '%$word%' OR name LIKE '%$word%' ORDER BY create_date DESC";
}
或者您可以构建一个长查询:
foreach($arr_words as $word){
$sql .= " OR name LIKE '%$word%' and description LIKE '%$word%'... ";
}
关于php - IOS 应用程序、搜索框和 SQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28401527/