2016-04-15 31 views
-1

我最近添加了搜索框來搜索數據庫中的某些數據。 我使用php LIKE function.for第一次當我使用LIKE函數只需要一個單詞,如果匹配的任何內容然後結果出來。如果我鍵入多於一個單詞它來到零結果,它返回一個錯誤,「沒有結果」。 當我做了一個研究,我做了我它的代碼的東西(現在是百達對我們搜索輸入該句子的最後一個單詞只搜索。) 在一個例子:我網站上的搜索框始終只搜索輸入句子的最後一個單詞

如果輸入「汽車出售「查詢將搜索」銷售「。如果有任何記錄匹配查詢,那麼它會exicutue(天氣它是汽車或任何其他數據)。

我想做什麼iss讓它搜索用於在數據庫中匹配的完整搜索。

對於一個例子:

,如果我想找到「爲賣汽車」 我想查詢我的汽車類等只執行對汽車銷售。

這是我的代碼。

$getquery = $_GET['query']; 

if(isset($_GET['query'])){ 

     $getquery = $_GET['query']; 
     $queryexplode = explode(" ", $getquery); 
      foreach($queryexplode as $queryexplodenew){ 
       $meta = $queryexplodenew; 
      } 

    $getdataquery = "SELECT `AdName`, `AdAccessLink`, `timestamp`, `Country`, `Category`, `Price`, `Photo1name` FROM freeadpostingdata WHERE AdName LIKE '%$queryexplodenew%' OR Country LIKE '%$queryexplodenew%' OR Category LIKE '%$queryexplodenew%' OR AdDescription LIKE '%$queryexplodenew%' ORDER BY timestamp DESC LIMIT 0, 25"; 
    $resultsgetdata = mysql_query($getdataquery); 
    $countprodu= mysql_num_rows($resultsgetdata); 

    } 

請幫我解決這個問題。 謝謝

回答

0

是的,對於您的代碼,它將始終搜索最後一個單詞。

$getquery = $_GET['query']; 
$modifiedKeyword = explode(" ", $getquery); 
$str = implode("', '", $modifiedKeyword); 

,改變你的查詢如下所示:

$sql = "SELECT id FROM your_table WHERE keyword IN ('$str')"; 

我希望它會爲你工作。

+0

我剛剛修改它,現在什麼都沒有發生。 –

+0

$ getquery = $ _GET ['query']; \t \t \t $ getquery = $ _GET ['query']; $ queryexplode = explode(「」,$ getquery); $ modifiedKeyword = implode(「','」,$ queryexplode); \t \t \t \t $ \t getdataquery =「SELECT'AdName','AdAccessLink','timestamp','Country','Category','Price','Photo1name' FROM freeadpostingdata WHERE AdName IN( '$ modifiedKeyword') OR國家IN('$ modifiedKeyword')或類別IN('$ modifiedKeyword')或者廣告描述IN('$ modifiedKeyword')ORDER BY timestamp DESC LIMIT 0,25「; –

+0

即時通訊它使用最新的codes.But它是相同的 –

1

的原因,你只搜索最後一個字是因爲這樣的:

$queryexplode = explode(" ", $getquery); 
     foreach($queryexplode as $queryexplodenew){ 
      $meta = $queryexplodenew; 
     } 

您遍歷每個字,每個字被放在$queryexplodenew依次查詢(然後出於某種原因分配給$meta)。循環完成後,$queryexplodenew中的內容將成爲迭代的最後一個元素。