How do I show the results of a certain year?

Asked

Viewed 50 times

-1

I’m trying to make a code where it only shows the results of a year but so nothing appears.

    <?php

    $conn = mysqli_connect('localhost','root','','formulario');

    $resultado = mysqli_query($conn, "SELECT * FROM dados WHERE data=2019");
    $r = mysqli_num_rows($res);

    while($r = mysqli_fetch_assoc($res)){

  $id=$r['id'];
  echo '<tr>';
  echo '<td>'.$r['id'].'</td>';
  echo '<td>'.$r['data'].'</td>';
  echo '<td>' .$r['escola'].'</td>';
  echo '<td>'.$r['nome'].'</td>';
  echo '<td>'.$r['processo'].'</td>';
  echo '<td>'.$r['email'].'</td>';
  echo '</tr>';
}
  echo '</table>';
 ?>

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • What is the table structure in the database? What is the value of $linhas? What are the latest messages in your server log file?

  • I’ve already modified but still nothing appears, stay blank

  • Your table does not have a column called dataenvio

  • yes I’ve changed tmb but it’s the same

  • And again, what is the value of $linhas and what are the latest log messages from your server?

  • I’ve already changed the value $lines to $r

  • Obs.: The column type data is datetimr, which indicates that it has a date and time. You are only comparing the value with a year.

  • If you change to date it will work?

  • I recommend you read the documentation on the types before and understand what you are doing than go out testing random changes.

  • and where can I find it?

  • ja resolvi era adicionar like e o ano $resultado = mysqli_query($Conn, "SELECT * FROM dados WHERE datasending LIKE '%2018%'");

Show 6 more comments

1 answer

1


You must use the function YEAR to take only the year of the date and compare with the desired value.

The problem is in the condition of sql:

... WHERE data = 2019

The right thing would be:

... WHERE YEAR(data) = 2019
  • pos thus $result = mysqli_query($Conn, "SELECT * FROM data WHERE datasending LIKE '%2018%'");

  • power can... but I believe the right way is by using YEAR, I don’t think LIKE be indicated to work with the type date, although it works...

Browser other questions tagged

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