IOS应用程序,搜索框和SQL数据库

时间:2015-02-09 01:31:46

标签: php ios mysql objective-c sql-server

我有一个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);

    }

}

1 个答案:

答案 0 :(得分: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%'... ";
}