php - MySQL + PhP - 搜索多个单词

标签 php mysql string

早上好!

想要使用将在 html 输入上传递的多个单词执行搜索。

示例:

  • 如果输入“gfilgueiras”将返回3条记录。
  • 如果输入“informativo gfilgueiras”将返回2条记录。
  • 如果输入“gfilgueiras::2”将仅返回1条记录。

我直接搜索 viewLogs。

我尝试了 concat,但没有成功。

如果我的英语不好,我很抱歉,我正在使用翻译器。

示例数据:

Screenshot

谢谢大家。

最佳答案

您想要的不可能,您需要在 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/

相关文章:

c - 从文件中找出哪一行元音字母最多

python - 输入和打印

php - 如何用php在jquery中添加删除功能?

php - mysql如何在foreach循环中左连接和回显变量

mysql - 如何在node.js中组合mysql select语句?

mysql - mysql服务器崩溃,并报告表问题

python - Python 中 isnumeric 和 isdecimal 的区别

php - command exec 和 php shell_exec 有不同的结果

php - 如何使用 TinyMCE textarea 编辑器作为一个类?

PHP session_start 失败