PHP / MYSQL Database Search Question

Now I just use simple

WHERE name LIKE '%$ser%'

But I ran into a problem - let's say search is testing 123, and "name" is "Testing", it does not return with any results. Do you know to fix this? Am I doing something wrong?

+3
source share
6 answers

If you want to find "Testing" or "123", use OR:

WHERE (name LIKE '%Testing%' OR name LIKE '%123%')

, , , , (, "4123" ). , Lucene, .

+5

LIKE - , , "%", - .

, , :

SELECT * FROM `Table` WHERE  "Testing 123" LIKE CONCAT("%",`name`,"%") 
+3

$ser :

WHERE name LIKE '%Testing 123%'

, :

WHERE name LIKE '%$word[1]%$word[2]%'

+1

, , , . , $ser = "testing" name = testing 123,

, , OR mysql full text search

+1

( ), , :

WHERE name LIKE '%$ser%' OR '$ser' LIKE CONCAT('%', name, '%')
+1

, , .

OR, , :

NOTE. Introductory disinfection and preparation are not indicated.

1 . Request with:

$sql_where = "WHERE name LIKE '%$ser%'";  

2. If zero results are returned, ask the user if they want to query each word separately.

3. If the user requests a search for “every word,” query with:

$sql_where = get_sql_where($ser);

(working) Sample code below:

$ser = 'Testing 123';
$msg = '';

function get_sql_where($ser){
    global $msg;
    $sql_where = '';
    $sql_where_or = '';

    $ser = preg_replace("/[[:blank:]]+/"," ", trim($ser)); //replace consecutive spaces with single space
    $search_words = explode(" ", $ser);

    if($search_words[0] == ''){
        $msg = 'Search quested was blank.';
    }else{
        $msg = 'Search results for any of the following words:' . implode(', ', $search_words);
        $sql_where = "WHERE name LIKE '%$ser%'";
        foreach($search_words as $word){
            $sql_where_or .= " OR name LIKE '%$word%'";
        }
    }

    return $sql_where . $sql_where_or;
}

$sql_where = get_sql_where($ser);
//Run query using $sql_where string
0
source

Source: https://habr.com/ru/post/1751652/


All Articles