Search records by criterion shows nothing

1

I do this query in PHP / MySQL and I want the records to be sorted by the nom field but it shows nothing.

$stm = $BD->prepare("SELECT a.cod, a.nom, b.id_pago, c.planilla
FROM sc_personal a,
sc_condicion b,
sc_planilla c
WHERE  a.id_condicion = b.id_condicion
AND  a.id_trab = c.id_trab
AND  a.cod ='.$valor'. ORDER BY a.nom ASC");

Before ending in:

AND  a.cod =".$valor);

And there was no problem, now I added the ORDER BY and it shows nothing. What is the problem?

    
asked by Piropeator 05.09.2017 в 00:11
source

2 answers

4

You should not concatenate values in prepared statements. The values should be linked. You also have a concatenated error.

Example of how it could be:

$stm = $BD->prepare("SELECT a.cod, a.nom, b.id_pago, c.planilla
FROM sc_personal a,
sc_condicion b,
sc_planilla c
WHERE  a.id_condicion = b.id_condicion
AND  a.id_trab = c.id_trab
AND  a.cod = ? ORDER BY a.nom ASC");

// Ligamos el valor
$stmt->bind_param('s', $valor);
// Ejecutamos
$stmt->execute();
    
answered by 05.09.2017 / 00:23
source
0

You are concatenating badly, try this way:

$stm = $BD->prepare("SELECT a.cod, a.nom, b.id_pago, c.planilla
                     FROM sc_personal a,
                          sc_condicion b,
                          sc_planilla c
                     WHERE  a.id_condicion = b.id_condicion
                     AND  a.id_trab = c.id_trab
                     AND  a.cod ='" . $valor . "' ORDER BY a.nom ASC");
    
answered by 05.09.2017 в 00:12