简体   繁体   中英

MySQL + PhP - search multiple words

Good morning!

Would like to perform a search with several words that will be passed on the html input.

Examples:

  • If you enter "gfilgueiras" will return 3 records.
  • If you enter "informativo gfilgueiras" will return 2 records.
  • If you enter "gfilgueiras :: 2" will return only 1 record.

I do a direct search for viewLogs.

I tried the concat, but did not succeed.

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.

// $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'";
                 }
             }
         }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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