How to perform an sql query with the LIKE clause using queries prepared in php?

1

I would like to know how I can perform a query prepare in mysql using the LIKE clause to filter

$stmt = $this->db->prepare("SELECT * FROM ARTICULOS WHERE DESARROLLO LIKE :search ");
        $search_ = '%'.$name.'%';
        $stmt->bindParam(":search",$search_,\PDO::PARAM_STR);
        $stmt->execute();
    
asked by Jhon Dember Murillo Mendez 26.04.2018 в 15:35
source

1 answer

1

Try the following:

$stmt = $db->prepare("SELECT * * FROM ARTICULOS WHERE DESARROLLO LIKE :search");
$search = '%SUVALOR%';
$stmt->bindValue(':search', $search, PDO::PARAM_STR);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

Another example:

You have to include the signs % in $params :

$query = "SELECT * FROM tabla WHERE direccion LIKE ? OR direccion LIKE ?";
$params = array("%$valor1%", "%$valor2%");
$stmt = $handle->prepare($query);
$stmt->execute($params);

If you look at the query generated in this previous code, it would look something like: SELECT * FROM tabla WHERE direccion LIKE '%"calle1"%' OR direccion LIKE '%"calle2"%' , because the prepared statement is quoting its values within a string already mentioned.

    
answered by 26.04.2018 в 15:46