简体   繁体   中英

how to perform a multi sql search via php in one field

i am using this sql search to find a title and artist in my database. I have on field containing infos like "ref.1570 title artist.mp4". When I do the search it works but in one direction only, i would like to get the result whatever the order i do the search... to be more precise if i search "title artist" no problem i found it. If i search "artist title" no way ... how can you help me making php sql search both directions ?

Best regards and thank you for your help. Phil

i am using this code :

 if ($search != null) {
        $sql = 'SELECT * FROM `catalog` WHERE (`file`LIKE "%' . $search . '%")';
        $sqlCount = 'SELECT count(*) FROM `catalog` WHERE (`file`LIKE "%' . $search . '%")';
    }

Why don't you split keyword $search with space and append with Like statement in the query. Eg:

if ($search != null) {
    $keywords=explode(" ",$search);
    $sql = 'SELECT * FROM `catalog` WHERE 1=1'; // just to append OR condition.This won't affect anything as the condition will always be true.
    $sqlCount = 'SELECT count(*) FROM `catalog` WHERE 1=1 ';
    foreach($keywords as $key){
        if(!empty($key)){
            $sql.=' And file Like \'%'.$key.'\%';
            $sqlCount.=' And file Like \'%'.$key.'\%';
        }
    } 

}

I believe this will work as you expected.

I think you won't get around a foreach:

if ($search != null) {
    $arrayOfSearchTerms = explode(' ', $search);

    $sql = 'SELECT * FROM `catalog` WHERE ';
    $sqlCount = 'SELECT count(*) FROM `catalog` WHERE ';
    $termNumber = 0;

    foreach ($arrayOfSearchTerms as $term) {
        $addingSql = '';
        if ($termNumber > 0) {
            $addingSql = ' OR ';
        }
        $addingSql .= '(`file` LIKE "%') . $term . '%")';
        $sql .= $addingSql;
        $sqlCount = $addingSql;
        $termNumber++;
    }
}

You need to iterate over your search terms and add this terms into your 'LIKE'-Statement

you can try this research: it does a LIKE %word% for every word. If you want more powerfull research you should use tools like elasticsearch etc... This way you could do:

$parts = explode(" ",$search)
$queryParts = array();

foreach ($parts as $part) {
   $queryParts[] = `artist`LIKE "%' . $part . '%" ;
}
// this way we can have like %artist% AND like %title%
$querySearch= implode(" AND ", $queryParts); 

$sql = 'SELECT * FROM `catalog` WHERE (". $querySearch .")';
$sqlCount = 'SELECT count(*) FROM `catalog` WHERE (`". $querySearch .")';

you have solved my problem i know now hos to correctly explode a request into variables ..

$search = $name;
  $explode=explode(" ",$search);

  print_r (explode(" ",$explode));

and then i use my sql request like this

$sql = 'SELECT * FROM `catalog` WHERE (`idc` LIKE "%' . $search . '%" OR `file` LIKE "%' . $search . '%" OR `title` LIKE "%' . $explode[0] . '%" AND `artist`LIKE "%' . $explode[1] . '% " OR `artist`LIKE "%' . $explode[0] . '%" AND `title`LIKE "%' . $explode[1] . '%")';

and it is working !

COOL

Use the multi query functions,

in mysqli should be : mysqli_multi_query();

More info on : Mysqli Multiquery

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