2015-11-29 163 views
-1

>這裏是代碼如何在Mysql數據庫中搜索多個詞搜索

i want to search the every word in my database but the results only show with single keyword not the multiple. it is a PDO database when i search single word like facebook then the result is appear if i search two or more word search then the result is not appear.

'

function getResults(){ 
$q=$GLOBALS['q']; 
$p=$GLOBALS['p']; 
$start=($p-1)*10; 
if($p!=null){ 
    $starttime = microtime(true); 
    $sql=$GLOBALS['dbh']->prepare('SELECT title, url, description FROM search WHERE `title` LIKE :q OR `url` LIKE :q OR `description` LIKE :q '); 
    $sql->bindValue(":q", "%$q%"); 
    $sql->execute(); 
    $trs=$sql->fetchAll(PDO::FETCH_ASSOC); 
    $endtime = microtime(true); 
    if($sql->rowCount()==0 || $start>$sql->rowCount()){ 
    return 0; 
    }else{ 
    $duration = $endtime - $starttime; 
    $res=array(); 
    $res['count']=$sql->rowCount(); 
    $res['time']=round($duration, 4); 
    $limitedResults=array_slice($trs, $start, 12); 
    foreach($limitedResults as $r){ 
    $res["results"][]=array($r['title'], $r['url'], $r['description']); 
    } 
    return $res; 
    } 
} 
} 
?> 

'

+0

你如何傳遞多個單詞?在1變量? – davejal

+0

你的意思是我添加另一個變量..? – Altamash

+0

你想讓你的搜索搜索Facebook並在域中鏈接嗎?如果是的話,你想如何將它們傳遞給您的查詢 – davejal

回答

-1

這裏是你發現有用的完整腳本。

<?php 
       if(isset($_GET["search"])) 
       { 
         $condition = ''; 
         //$query = explode(" ", $_GET["search"]); 
         $query = explode(" ", $_GET["search"]); 

         foreach($query as $text) 
         { 
          $condition .= "`title` LIKE +'%".mysqli_real_escape_string($connect, $text)."%' OR "; 
         } 
         $condition = substr($condition, 0, -4); 
         $sql_query = "SELECT * FROM countries2 WHERE " . $condition; 
         $result = mysqli_query($connect, $sql_query); 
         if(mysqli_num_rows($result) > 0) 
         { 
          while($row = mysqli_fetch_array($result)) 
          { 
           echo '<tr><td>'.$row["title"].'</td></tr>'; 
          } 
         } 
         else 
         { 
          echo '<label>Data not Found</label>'; 
         } 
       } 
       ?> 
0

你必須建立一個動態的查詢來搜索兩個詞。在下面的代碼中,我演示了使用GET來顯示查詢和數組的構建查詢。我儘可能使用未命名的參數?和「懶惰」綁定 - 將數據傳遞到執行中以方便使用。

//Using GET here for testing 

<?php 
//Using GET here for testing 
if(isset($_GET['q'])){ 
$q = $_GET['q']; 
//} 
//First split string 
if (strpos($q, ' ') > 0) { 
$pieces = explode(" ", $q); 
//echo count($pieces); 
//You need an array to store columns 
$columnArray = array("title", "url", "description"); 
//You need an array to store parameters 
$paramArray =array(); 
$clause = "WHERE"; 
} 
//You need to build a dynamic query to perform this. Start with a basic stub 

    $sql = "SELECT `title`, `url`, `description` FROM search "; 

//Start building the query 
//Also see PDO WIKI for use % in placeholders.ie placeholders cannot represent an arbitrary part of the query, but a complete data literal only 
//Loop through columns 
     foreach ($columnArray as $column) { 
      //$sql . 
      foreach ($pieces as $value) { 
       $sql .= " $clause $column LIKE ?"; 
      $clause = "OR"; 
      array_push($paramArray,"%$value%"); 
      } 
} 

//echo query and parameter array 
echo $sql; 
echo "<br>"; 
print_r($paramArray); 

//Prepare and execute query 
// $sth = $db->prepare($sql); 
// $sth->execute($paramArray); 
} 
?> 

導致 xxx.php Q =谷歌亞馬遜Quora的 或xxx.php?Q =谷歌+亞馬遜+ Quora的

SELECT `title`, `url`, `description` FROM search WHERE title LIKE ? OR title LIKE ? OR title LIKE ? OR url LIKE ? OR url LIKE ? OR url LIKE ? OR description LIKE ? OR description LIKE ? OR description LIKE ? 
Array ([0] => %google% [1] => %amazon% [2] => %quora% [3] => %google% [4] => %amazon% [5] => %quora% [6] => %google% [7] => %amazon% [8] => %quora%) 
+0

謝謝你的答案,但輸出在瀏覽器中的錯誤。 SELECT title,url,description from search where'title' like google or'url' like Google Array([0] =>%google%[1] =>%google%) – Altamash

+0

我也在使用PDO可以說這是一個小型的搜索引擎和即時通訊使用MYISAM數據庫? – Altamash

+0

您是否嘗試過給出的代碼?你好像修改了** WHERE'title' LIKE?**。你必須保留**?** –

1

不知道爲什麼你沒有使用@大衛斯特拉坎(我認爲這是一個很好的解決方案),但我會嘗試解釋一個選項,您可能也會使用並理解爲什麼當前沒有獲得任何結果。

在這一點上會發生的是,你發送這些值到你的查詢,最終成爲;

SELECT title, url, description FROM search WHERE `title` LIKE `google+facebook+yahoo` OR `url` LIKE `google+facebook+yahoo` OR `description` LIKE `google+facebook+yahoo` 

我不認爲你想找一個包含所有這些的標題。

我不確定如何使用查詢來編寫此內容,但對您而言,快速解決方案可能會是這樣。

  1. 插入值到一個數組(谷歌+實+雅虎)
  2. 使用循環來選擇從創建的陣列中的每個鍵,並傳遞到查詢
  3. 使查詢添加它的結果作爲一個陣列