I pass a parameter to the function and I want to use it in a regular expression, but this code does not work and does not generate errors. If instead of ':code%' I write '$code%' , i.e. just passing the variable, then the code runs as it should. I want to use exactly the prepared request. Prompt the correct syntax.

 function codeFindPart($code) { //... $sql="SELECT code_1 FROM fkko_id WHERE code_1 LIKE ':code%' "; $result=$pdo->prepare($sql); $result->bindParam(":code",$code,PDO::PARAM_STR); $result->execute(); //... } 

    2 answers 2

    At the request level is also not a problem, it is only necessary to take into account that the PDO substitutes a self-sufficient parameter, i.e. You will have a piece of text. Here you need to paste the % symbol to the text, for which there is a concat function:

     WHERE code_1 LIKE concat(:code, '%') 
    • Thank you, this is exactly what you need) - Vincent

    Change not the request, but the variable itself.

     function codeFindPart($code) { //... $sql="SELECT code_1 FROM fkko_id WHERE code_1 LIKE :code "; $result=$pdo->prepare($sql); $result->bindParam(":code", "$code%", PDO::PARAM_STR); $result->execute(); //... } 

    Read here , useful.

    • good decision too, thank you - Vincent