Problem with mysql and php


I have the following doubt, I am doing a search engine, that I look for in my database a product that contains some letter in its name, I make the query in phpmyadmin and all good, it works for me, the problem comes when I pass by parameter from the input, and I do not know how to make the parameter pass through the bindParam, here I leave the code, could you give me a hand? It's not been long since I started with this php, thank you very much!

$Buscar= $_POST{'busca'};

$sql ="select p.idProducto, p.nombre_producto, p.precio, c.nombre_categoria 
       as categoria, p.stock, p.detalles from productos p join categorias
       c on c.id_categoria = p.id_categoria where p.idEstadoEliminacion = 1
                      and nombre_producto like '%:busca%'";
                    $sentencia = $con->prepare($sql);
                    $sentencia-> bindParam(':busca', $Buscar, PDO::PARAM_STR);
asked by GALS 09.09.2018 в 07:47

2 answers


You can try replacing the last part of your query with:

and p.nombre_producto like CONCAT('%', :busca, '%')";
answered by 09.09.2018 / 08:18

Try this way

and nombre_producto like %:busca% ";

$sentencia-> bindParam(':busca', $Buscar);
answered by 09.09.2018 в 16:13