我有一個表單,我希望用戶輸入一個或多個單詞。這些單詞應該匹配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
而我想匹配我的搜索字符串與兩列的內容..我會如何繼續?
在這一點上,你會想要對條款進行「foreach」並以這種方式建立你的條件。 – keithhatfield 2012-07-18 14:03:08
@dleiftah你能舉個例子嗎? – David 2012-07-18 14:06:27