sql return php data

Asked

Viewed 32 times

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 search in the mysql table to pick up and count all today’s sales to later insert this in a chart, but I can’t print anything. can help me?

  • I believe you can take the if($linha['dataCompra'] == $data_hoje){ because you already made the filter in the query. I don’t know if the query is right.

  • If I take the if, it will not count to show it on google Chart...

  • and leave only the counter?

  • without success.. the way I did, it returns on the chart only today’s date 26, but it does not search in the comic how many sales were made today ..

2 answers

0

You can use the CURDATE() instead of NOW() (now 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);
    }
}
  • How do I show this? because the variable $today will receive this information, and then it will be adding ++ alone to later go to the chart of Charts..

0

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

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

"SELECT *  FROM vendas WHERE date(dataCompra) = date(now())"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.