Search code examples
phpmysqlpdobindparam

Like query with alphanum string bindParam


I´m new working with pdo statements and when I try to set a query like this in phpMyadmin sql console, it´s works fine:

SELECT name, number FROM client WHERE number LIKE '%30%'

Result:

|name|number|
|antonio|30857898H|
|andrés|30987454U|

But when I pass the params with the method bindParam() or the method bindValue() or with the type value like PDO::PARAM_STR or PDO::PARAM_INT, the behaviour is that the query stop in the first number, and don´t get the other characters. The cols are both 'char'...

This is my method:

function getQueryData($key,$i_query){

    require_once '../queryList.php';//get all the querys
//$key is an array like this $key[0][0] = ':number', $key[0][1] = '%30%'    
    $this->consulta = $this->conInstance->prepare($gQuery[$i_query]);

    foreach ($key as $clave => $valor) {
        $this->consulta->bindParam($key[$clave][0], $key[$clave][1]);
        }       
    $this->consulta->execute();

    $this->consulta->setFetchMode(PDO::FETCH_ASSOC);

    return $this->consulta;
    //Make the bucle on the system
}

Thaks a lot for the help in this comunity, the people here are fantastic


Solution

  • try isolate the param content in the like matching clause as

    SELECT name, number FROM client WHERE number LIKE concat('%', '30', '%')
    

    eg

    SELECT name, number FROM client WHERE number LIKE concat('%', :your_param , '%')
    

    or equivalent