SQL query in PHP with LIKE

0

Good morning, I'm doing the following query in PHP, do not send me any errors but do not throw anything at me as results.

    $sql = $conn->prepare("SELECT Nombre, Campania, Puesto, Turno FROM usuarios
INNER JOIN campania ON usuarios.IdCampania=campania.IdCampania
INNER JOIN puesto ON usuarios.IdPuesto=puesto.IdPuesto
INNER JOIN turno ON usuarios.IdTurno=turno.IdTurno
WHERE Nombre LIKE '%':nombre'%' ");
$sql->execute(array(':nombre'=>$nombre));
$rs = $sql->fetchAll();

<table class="mitabla col-12 col-m-6">
    <tr>
        <th>Nombre</th>
        <th>Campaña</th>
        <th>Puesto</th>
        <th>Turno</th>
    </tr>
    <?php foreach($rs as $resultado): ?>  
    <tr>
        <td><?php echo $resultado['Nombre'];?></td>
        <td><?php echo $resultado['Campania'];?></td>
        <td><?php echo $resultado['Puesto'];?></td>
        <td><?php echo $resultado['Turno'];?></td>
    </tr>
    <?php endforeach; ?>
</table>

I hope you can help me. Thanks.

    
asked by Guillermo Ricardo Spindola Bri 25.05.2017 в 20:06
source

3 answers

1

Ready, I'm just adding an additional space between the placeholder and the percent signs

    $sql = $conn->prepare("SELECT Nombre, Campania, Puesto, Turno FROM usuarios
INNER JOIN campania ON usuarios.IdCampania=campania.IdCampania
INNER JOIN puesto ON usuarios.IdPuesto=puesto.IdPuesto
INNER JOIN turno ON usuarios.IdTurno=turno.IdTurno
WHERE Nombre LIKE '%' :nombre '%' ");
$sql->execute(array(':nombre'=>$nombre));
$rs = $sql->fetchAll();
    
answered by 25.05.2017 в 20:38
0

I would say that the problem has it in where . You're having me search for '%'nombre'%' that would translate to any name that contains 'CE' (for example)

You do not have to put the % in quotes. It would be '%:nombre%'

    
answered by 25.05.2017 в 20:22
0

Hi, you could try putting the name of the table before the field to select something like SELECT usuario.Nombre, compania.Campania... and so for the others

    
answered by 25.05.2017 в 20:54