Like query with alphanum string bindParam

Viewed 75

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

2 Answers

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

With bind parameters you need pass whole string with '%'

query:

SELECT name, number FROM client WHERE number LIKE :your_param

code:

$value = 'some value';
$this->consulta->bindParam('your_param', '%'.  $value . '%');
Related