sql return given php

0
$hoje = date('d');
$data_hoje = "SELECT *  FROM vendas WHERE dataCompra = now()";
$result = mysqli_query($conexao, $data_hoje);

while($linha = mysqli_fetch_assoc($result)){    
    if($linha['dataCompra'] == $data_hoje){
        $hoje++;
    }
}

I'm doing a mysql table search to pick up and count all sales today and then insert that into a chart, but I can not print anything. can you help me?

    
asked by anonymous 26.10.2018 / 20:44

2 answers

0

You can use CURDATE() instead of NOW() now get date + time):

$data_hoje = "SELECT * FROM vendas WHERE DATE_FORMAT(dataCompra,'%d/%m/%Y') = CURDATE()";

You can use DATE_ADD() for this control; as the field is a date, it is interesting to compare them by formatting:

$hoje = DATE_FORMAT(date(),'%d/%m/%Y');
$data_hoje = "SELECT *  FROM vendas WHERE DATE_FORMAT(dataCompra,'%d/%m/%Y') = CURDATE()"";
$result = mysqli_query($conexao, $data_hoje);

while($linha = mysqli_fetch_assoc($result)){    
    if(DATE_FORMAT($linha['dataCompra'],'%d/%m/%Y') == DATE_FORMAT($data_hoje,'%d/%m/%Y')){
        $hoje = DATE_ADD($hoje, INTERVAL 1 DAY);
    }
}
    
26.10.2018 / 20:48
0

You are comparing datetime, when you use campo = now() you are comparing date and time.

Try to give a date() in the fields of your select like this:

"SELECT *  FROM vendas WHERE date(dataCompra) = date(now())"
    
26.10.2018 / 22:23