Query criteria with wildcard

0

I'm trying to use wildcard character as query criteria in my query, but the result is returned empty.

include_once ("../ inc / connection.php");

$lj = "4";
$linha      = isset($_GET['ans'])?$_GET['ans']:"";
$data   = date("d-m-Y");
$partes = explode("-", $data);
$ano    = $partes[2];
$periodo = "$ano%";


$sql = "SELECT sjy_vendas.tipo, Sum(sjy_vendas.qt) AS qt, Sum(sjy_vendas.qtv) AS qtv, Sum(sjy_vendas.rprd) AS rprd, Sum(sjy_vendas.rsrv) AS rsrv, Sum(sjy_vendas.tprd) AS tprd, Sum(sjy_vendas.tsrv) AS tsrv, Sum(sjy_vendas.vlr_compra) AS vlr_compra
FROM sjy_vendas
WHERE sjy_vendas.empresa = $lj 
AND sjy_vendas.tipo = $linha
AND dt_nf CONCAT('%',:param,'%')
GROUP BY sjy_vendas.tipo";
$sql->bindParam(':param',$ano);
$consulta   = mysqli_query($conexao,$sql);
while($dados = mysqli_fetch_array($consulta)){

$qtv    = $dados['qtv'];
$rprd   = $dados['rprd'];

}

echo "Quantidade: " . $qtv . "<br />";
echo "Receita: " . $rprd . "<br />";
    
asked by anonymous 20.10.2017 / 00:38

2 answers

0

Change the sql variable to a mysqli_prepare function call by passing the sql string as a parameter then use the msqli_bind_param function to append the $ ano parameter. Here is an example, you will have to adapt it to work according to your needs:

$link = mysqli_connect("localhost", "my_user", "my_password", "world");
$sql = mysqli_prepare($link, "SELECT sjy_vendas.tipo, Sum(sjy_vendas.qt) AS qt, Sum(sjy_vendas.qtv) AS qtv, Sum(sjy_vendas.rprd) AS rprd, Sum(sjy_vendas.rsrv) AS rsrv, Sum(sjy_vendas.tprd) AS tprd, Sum(sjy_vendas.tsrv) AS tsrv, Sum(sjy_vendas.vlr_compra) AS vlr_compra
FROM sjy_vendas
WHERE sjy_vendas.empresa = $lj 
AND sjy_vendas.tipo = $linha
AND dt_nf CONCAT('%',:param,'%')
GROUP BY sjy_vendas.tipo""));
mysqli_stmt_bind_param($sql, "s", $ano);

More detailed information on this link: link

    
20.10.2017 / 16:47
0

If the problem is concatenation of the string '%' in the parameter's variable, you can try as in the following example using the SQL CONCAT function:

$sql = "SELECT item_title FROM item WHERE item_title LIKE CONCAT('%',:param,'%')";
$sql->bindParam(':param', $paramentro );
    
20.10.2017 / 01:03