2012-07-18 59 views
0

我有一個表單,我希望用戶輸入一個或多個單詞。這些單詞應該匹配MySQL數據庫中的多列。PHP爆炸和MySQL查詢在多列中搜索

我已經開始構建一些代碼,但我卡住了。

<?php 
    $term = $_SESSION['session_searchstring']; //Let's say that session is John Doe 
    $searchterm = explode(' ',$term); 

    $searchFieldName = "name"; 
    $searchCondition = "$searchFieldName LIKE '%" . implode("%' OR $searchFieldName LIKE '%", $searchterm) . "%'"; 

    $sql = "SELECT * FROM students WHERE $searchCondition;"; 

    echo $sql; //Echo to test what mysql_query would look like 

?> 

上面的代碼將輸出:

SELECT * FROM students WHERE name LIKE '%John%' OR name LIKE '%Doe%'; 

的問題是,我想在多個列($searchFieldName)來搜索。我有例如

customer_firstname 
customer_lastname 

而我想匹配我的搜索字符串與兩列的內容..我會如何繼續?

+0

在這一點上,你會想要對條款進行「foreach」並以這種方式建立你的條件。 – keithhatfield 2012-07-18 14:03:08

+0

@dleiftah你能舉個例子嗎? – David 2012-07-18 14:06:27

回答

1

也許

$term = $_SESSION['session_searchstring']; //Let's say that session is John Doe 
    $searchterm = explode(' ',$term); 

    $searchColumns = array("customer_firstname","customer_lastname"); 

    for($i = 0; $i < count($searchColumns); $i++) 
    { 
     $searchFieldName = $searchColumns[$i]; 
     $searchCondition .= "($searchFieldName LIKE '%" . implode("%' OR $searchFieldName LIKE '%", $searchterm) . "%')"; 
     if($i+1 < count($searchColumns)) $searchCondition .= " OR "; 
    } 

    $sql = "SELECT * FROM students WHERE $searchCondition;"; 

    echo $sql; //Echo to test what mysql_query would look like 

主要生產

SELECT * FROM students WHERE (customer_firstname LIKE '%John%' OR customer_firstname LIKE '%Doe%') OR (customer_lastname LIKE '%John%' OR customer_lastname LIKE '%Doe%');

+0

它開始看起來不錯!然而,我得到'未定義的變量:searchCondition' – David 2012-07-18 14:14:16

+0

嘗試在$ searchColumns行後面添加'$ searchCondition ='';' – Kermit 2012-07-18 14:15:22

+0

謝謝,現在工作了! – David 2012-07-18 14:16:48

1

如果您的表格是MyIsam類型,或者您可以將其轉換爲MyIsam,請使用MySQL Fulltext Search。如果不是,反正,你可以建立像

SELECT * FROM students WHERE name LIKE '%John%' OR name LIKE '%Doe%' OR lastname LIKE "%John%" OR lastname LIKE "%Doe%" 

或工會的列長查詢到彼此只是搜索(但是這兩者並不者優先)。 另一個好方法是使用全文搜索引擎,如Sphinx

0

在我來說,我需要所有的搜索短語/術語來匹配至少一列,沒有搜索短語/學期可能是一個不匹配。

我結束了從Kermit調整的例子以下列方式:

public function getRawWhereFilterForColumns($filter, $search_columns) 
{ 
    $search_terms = explode(' ', $filter); 
    $search_condition = ""; 

    for ($i = 0; $i < count($search_terms); $i++) { 
    $term = $search_terms[$i]; 

    for ($j = 0; $j < count($search_columns); $j++) { 
     if ($j == 0) $search_condition .= "("; 
     $search_field_name = $search_columns[$j]; 
     $search_condition .= "$search_field_name LIKE '%" . $term . "%'"; 
     if ($j + 1 < count($search_columns)) $search_condition .= " OR "; 
     if ($j + 1 == count($search_columns)) $search_condition .= ")"; 
    } 
    if ($i + 1 < count($search_terms)) $search_condition .= " AND "; 
    } 
    return $search_condition; 
} 

,因爲我使用Laravel並可能將其寫入rawWhere法我只需要在WHERE子句的內容。

用法:

$search_condition = $this->getRawWhereFilterForColumns 
    ("John Doe", array("column1", "column2")); 

產生

(column1 LIKE '%John%' OR column2 LIKE '%John%') 
AND (column1 LIKE '%Doe%' OR column2 LIKE '%Doe%') 

最後你以任何方式適合你,例如使用$ search_condition:

$sql = "SELECT * FROM students WHERE $search_condition;"; 

或者在我的La拉威爾情況:

$modelInstances = Model::whereRaw 
    ($search_condition)->paginate(self::ITEMS_PER_PAGE); 

也許這是無論David或其他人的改進方案別的訪問此線程,像我這樣的,即使是近兩年後,原來的職位。