簡體   English   中英

ANDing像通配符搜索一樣,使用通過mysqli准備的語句包含的多個關鍵字

[英]ANDing Like wildcard search using multiple keywords contained via mysqli prepared statement

嘗試使用mysqli准備的語句使用(LIKE CONCAT('%',?,'%')AND)而不是(LIKE CONCAT('%',?,'%')OR)進行搜索使用'call_user_func_array'代替,但盡管可以這樣做,但僅在標題中包含分手搜索字符串中的任何一個單詞時,它才返回記錄,我需要它僅返回所有單詞都包含在標題中的標題。標題。

<?php        
        $searchstr    = $_POST['inputsearch'];  //Input string from form
        $searchwords  = split(" ",$searchstr);  //Breaking the string to array of words
        $totalwords   = count($searchwords);    //Total words to search for
        $ind          = 1;
        while(list($key,$val)=each($searchwords))
            {
            if(trim($val) !="" && strlen($val) > 0){ // check good words
              $bindingstring[$ind] = $val;            // create new array of words
              $ind++;
              }
            }
        $callsqli = $conn->prepare("SELECT mytitle,myad_id FROM classifieds WHERE mytitle like CONCAT('%', ?, '%')"); //prepare with wildcards
        if(!$callsqli) {
                      echo 'Wrong SQL: Error: ' . $conn->error, E_USER_ERROR; // oops
                      return 0;
                    }
        for($i=1; $i<=$totalwords; $i++){
          $callsqli->bind_param('s',$bindingstring[$i]);            // bind each value
          $callsqli->execute();
                }
          $callsqli->store_result();
          $callsqli->bind_result($mytitle,$myadid);                 // get each title and advert id
                while($callsqli->fetch()){                                 // fetch each result and display
            echo "<a href='http://www.somesite.com/someview.php?&someitem=".$myadid."'>".$mytitle."</a><br />";
                }
                $callsqli->free_result();
                $callsqli->close();
?>

我假設您的所有代碼都可以正常工作,但sql部分除外,因此您需要生成包含通配符綁定的sql字符串。 您可以通過遍歷word數組並將其附加到查詢字符串中來完成此操作。 生成查詢后,將執行查詢並獲取結果:

    $searchstr    = $_POST['inputsearch'];  //Input string from form
    $searchwords = preg_split('/\s+/', $searchstr); //preg_split with whitespaces and tabs !!!
    $bindingstring = array();

    foreach($searchwords as $key => $val){
        $temp = strval(trim($val));
        if($temp != "" && strlen($temp) > 0){
            $bindingstring[] = $temp;
        }
    }

    $sql = "SELECT mytitle,myad_id FROM classifieds WHERE ";

    foreach($bindingstring as $key => $value){// generate the SQL
        if($key > 0){
            $sql .= "AND ";//dont include AND in the first iteration
        }
        $sql .= "mytitle LIKE CONCAT('%', ?, '%') ";
    }
    echo $sql;
    $callsqli = $conn->prepare($sql); //prepare the query
    if(!$callsqli) {
        echo 'Wrong SQL: Error: ' . $conn->error, E_USER_ERROR; // oops
        return 0;
    }

    foreach($bindingstring as $key => $value){
      $callsqli->bind_param('s',$value);            // bind each value
    }

    $callsqli->execute();
    $callsqli->store_result();
    $callsqli->bind_result($mytitle,$myadid);                 // get each title and advert id

    while($callsqli->fetch()){// fetch each result and display
        echo "<a href='http://www.somesite.com/someview.php?&someitem=".$myadid."'>".$mytitle."</a><br />";
    }
    $callsqli->free_result();
    $callsqli->close();

編輯:我編輯了答案,使用preg_split通過空格或制表符分隔單詞以獲得更一致的行為,還清理了單詞的評估並進行修整以使其更易於閱讀

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM