2015-02-09 69 views
1

我有一個SQL虛擬主機數據庫與描述爲「說明」的表。現在數據庫可以完全正確地與我的應用程序對話,但我似乎在搜索中有多個關鍵字存在問題。截至目前,搜索只會在我的數據庫的表'Description'內的搜索框中找到輸入的第一個單詞。所以說,如果我的用戶通過應用程序中的搜索框輸入「龍紋身」,它將只顯示「龍」的所有內容。總之,什麼是讓我的搜索框列出任何帶有多個單詞的關鍵字的最佳方法完全匹配。IOS應用程序,搜索框和SQL數據庫

下面是搜索功能我的服務器端的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); 

    } 

} 

回答

1

我不知道有關性能爲這一個,但你可以用多個查詢嘗試:

$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%'... "; 
} 
+0

現在$ str將等於$ text或我的搜索功能。因爲我的主要問題是搜索不止一個單詞。我不一定要使用「龍紋身」,這只是一個例子 – pLokie 2015-02-09 03:42:17