Skip to content
Advertisement

SQL handle select with split strings

I have a request whom looks like this :

"SELECT  CLCLEUNIK
,NOM
,VILLE
,CODEP
FROM CLIENT 
WHERE NOM LIKE :nom COLLATE Latin1_general_CI_AI"

the :nom is a String, whom can be one word or many words.With php, I want my request to find all the possibility for each words and keep only those in common for each words, but I can’t find the way by myself.

I tried to split my string in a String[ ] and do the request for each words, but i don’t know how to stock the results in different variables.

Please help me. Thanks.


Edit

There is my php code :

  public function test(Request $request){
    require __DIR__.DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR."Entity".DIRECTORY_SEPARATOR."sqlconnect.php";
    $nom = $request->request->has("nom") ? $request->request->get("nom") : "";
    $mots[]=explode(" ", $nom);
    if (sizeof($mots)>1){
      for($i=0; $i>sizeof($mots); $i++){



      }
    }else{
      $req ="SELECT  CLCLEUNIK, NOM, VILLE, CODEP FROM CLIENT WHERE NOM LIKE :nom COLLATE Latin1_general_CI_AI";
      $prepare = $pdo->prepare($req);
      $prepare->execute(
        [
          "nom" => "%{$nom}%"
        ]
      );

      $clientsrecherche = $prepare->fetchAll(PDO::FETCH_OBJ);

      foreach($clientsrecherche as $key => $objet){
        foreach($objet as $key2 => $obj){
          $obj=utf8_encode($obj);
          $response[$key][$key2] = $obj;
        };
      }                     
        return new JsonResponse([ "response" => $response]);
    }
   }

I don’t know what to put in my for.

Advertisement

Answer

Juste needed to replace ” ” by “%” and it’s worked

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement