简体   繁体   English

MySQL + PhP-搜索多个单词

[英]MySQL + PhP - search multiple words

Good morning! 早上好!

Would like to perform a search with several words that will be passed on the html input. 想要使用将在html输入中传递的几个单词执行搜索。

Examples: 例子:

  • If you enter "gfilgueiras" will return 3 records. 如果输入"gfilgueiras"将返回3条记录。
  • If you enter "informativo gfilgueiras" will return 2 records. 如果输入"informativo gfilgueiras"将返回2条记录。
  • If you enter "gfilgueiras :: 2" will return only 1 record. 如果输入"gfilgueiras :: 2"将仅返回1条记录。

I do a direct search for viewLogs. 我直接搜索viewLogs。

I tried the concat, but did not succeed. 我尝试了concat,但没有成功。

I'm sorry if my English is not good, I'm using a translator. 如果我的英语不好我很抱歉,我正在使用翻译器。

Sample Data: 样本数据:

屏幕截图

Thank you all. 谢谢你们。

SELECT * FROM viewLogs WHERE concat(all field you want ) REGEXP "gfilgueiras"

What you want is not possible, you need to use AND on your WHERE clause, assuming the search word delimiter is space. 想要的是不可能的,您需要在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM