PDO using LIKE in the MySQL query

-1

I have the following code

$damn = '%'.$busqueda.'%';
            $q_i = $this->pdo->prepare('SELECT * FROM registro_viaje WHERE (id_pas_reg LIKE ? OR nomc_pas_reg LIKE ? OR tel_pas_reg LIKE ?) AND matricula_veh_reg=?');
            $q_i->bindParam(1,$damn);
            $q_i->bindParam(2,$damn);
            $q_i->bindParam(3,$damn);
            $q_i->bindParam(4,$matricula);
            $q_i->execute();
            $q_r = $q_i->fetchAll();
            if (count($q_r)>0) {
                print_r($q_r);
            }else{
                echo "Nada, papú :(";
            }

In the database, there is information about Felipe Solano , so $ search is equal to "Felipe", when you start the code it turns out that there are no results. .. NOTESE the use of LIKE , what am I missing here?

    
asked by Máxima Alekz 14.10.2016 в 01:50
source

1 answer

2

Try the following code:

$damn = "Tu busqueda";
$q_i = $this->pdo->prepare('SELECT * FROM registro_viaje WHERE id_pas_reg LIKE ? OR nomc_pas_reg LIKE ? OR tel_pas_reg LIKE ? AND matricula_veh_reg=?');
$q_i->bindValue(1,"%{$damn}%", PDO::PARAM_STR);
$q_i->bindValue(2,"%{$damn}%", PDO::PARAM_STR);
$q_i->bindValue(3,"%{$damn}%", PDO::PARAM_STR);
$q_i->bindValue(4,$matricula);
$q_i->execute();
$q_r = $q_i->fetchAll();
if (count($q_r)>0) {
    print_r($q_r);
}else{
    echo "Nada, papú :(";
}

Keep in mind that if the AND clause is not met, it will not bring anything from the DB. Greetings.

    
answered by 14.10.2016 / 02:31
source