早上好!
想要使用将在 html 输入上传递的多个单词执行搜索。
示例:
- 如果输入
“gfilgueiras”
将返回3条记录。 - 如果输入
“informativo gfilgueiras”
将返回2条记录。 - 如果输入
“gfilgueiras::2”
将仅返回1条记录。
我直接搜索 viewLogs。
我尝试了 concat,但没有成功。
如果我的英语不好,我很抱歉,我正在使用翻译器。
示例数据:
谢谢大家。
最佳答案
您想要的不可能,您需要在 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'";
}
}
}
}
}
关于php - MySQL + PhP - 搜索多个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23486145/