[英]MySQL + PhP - search multiple words
早上好!
想要使用將在html輸入中傳遞的幾個單詞執行搜索。
例子:
"gfilgueiras"
將返回3條記錄。 "informativo gfilgueiras"
將返回2條記錄。 "gfilgueiras :: 2"
將僅返回1條記錄。 我直接搜索viewLogs。
我嘗試了concat,但沒有成功。
如果我的英語不好我很抱歉,我正在使用翻譯器。
樣本數據:
謝謝你們。
SELECT * FROM viewLogs WHERE concat(all field you want ) REGEXP "gfilgueiras"
想要的是不可能的,您需要在WHERE子句上使用AND,並假設搜索詞定界符為空格。
// $searchWord is informativo gfilgueiras
if(!empty($searchWord)) {
// separates the searched word by space and puts it in array, if no space, put in array.
$searchWords = strpos($searchWord,' ') !== false ? explode(' ',$searchWord) : array($searchWord);
// this is just an example, use sql prepared statement for this
$sqlString = "SELECT * FROM viewLogs WHERE concat(ip,login, descricao, data, hora, acao, severidade) REGEXP '$searchWord[0]' ";
if(count($searchWords) > 0) {
if(isset($searchWords[0])) {
foreach($searchWords as $index => $searchWord) {
if($index > 0) {
// this is just an example, use sql prepared statement for this
$sqlString .= "AND WHERE concat(ip,login, descricao, data, hora, acao, severidade) REGEXP '$searchWord'";
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.