What’s wrong with this query?

Asked

Viewed 66 times

0

Okay, I’m trying to select a database through ajax using jQuery. Below are excerpts of the code I’m using and more explanations:

Ajax function

function carregaRegistros(dia, mes, ano) {

    dia = parseInt(dia);
    mes = parseInt(mes);
    ano = parseInt(ano);

    $.ajax({
        method: "POST",
        url: "PHP-Files/carregaRegistros.php",
        data: {dia: dia, mes: mes, ano: ano},
        success: function(result){
            alert(result);
        }
    });
}

Query

// Cria conexão
$conn = new mysqli($servername, $username, $password, $dbname);
// Verifica conexão
if ($conn->connect_error) {
    die("Erro de conexão: " . $conn->connect_error);
}

$dia = $_POST["dia"];
$mes = $_POST["mes"];
$ano = $_POST["ano"];

$sql = "SELECT Cliente, TotalDeHoras, HoraDeEntrada, HoraDeSaida FROM 
apontamentos WHERE Dia='$dia' AND Mes='$mes' AND Ano='$ano'";

if ($conn->query($sql) === TRUE) {
    echo "Apontamento incluído com sucesso!";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

I already checked and the connection is being made and the data is being passed correctly, however I get an error message as return which indicates that the select is not being done. I don’t understand why this is happening, and I’m kind of new at this part of creating database connections. If I have omitted any important information please ask in the comments

  • What error is being returned?

  • Error: SELECT Customer, Totaldehoras, Horadeentrada, Horadesaida FROM notes WHERE Dia='1' AND Mes='5' AND Ano='2018'<br>

  • If any answer solved your problem be sure to report it as accepted, see https://i.stack.Imgur.com/evLUR.png and because https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-accepta reply/1079#1079

2 answers

1

A select query will not return a true one, as in this comparison you are trying to do. You can even check if it will not return errors, but this comparison that is doing it will never enter the if. That is why your error does not return any error, because there are no errors... Voce can try to do so:

if ($conn->query($sql)) {}

I believe I’ve overcome your doubt.

  • Thanks, now I understand what was happening, but how can I get the result of this query? Because my php file was returning my texts, how can I make it return the query result?

  • In this case you need to fetch and mount the result to an array or a string and pass it.

1

The correct is to check if there is any record

select Count checks the number of non-zero lines within the count what you want to do!

  $result = $conn->query("SELECT COUNT(*) FROM apontamentos WHERE Dia='$dia' AND Mes='$mes' AND Ano='$ano''");

  $row = $result->fetch_row();

  if ($row[0] > 0) {
      echo "Apontamento incluído com sucesso!";
  } else {
      echo "Error: " . $sql . "<br>" . $conn->error;
  }

or

$result = $conn->query("SUA QUERY")); 
if($result->num_rows){
   // echo "Apontamento incluído com sucesso!";
}else{
   // echo "Error: " . $sql . "<br>" . $conn->error;
}

To return the records

$result = $conn->query("SUA QUERY")); 
if($result->num_rows){

   while($row = $result->fetch_assoc()) {
        $Cliente = $row["Cliente"];
        $TotalDeHoras = $row["TotalDeHoras"];
        $HoraDeEntrada = $row["HoraDeEntrada"];
        $HoraDeSaida = $row["HoraDeSaida"];

     }

   // echo "Apontamento incluído com sucesso!";
}else{
   // echo "Error: " . $sql . "<br>" . $conn->error;
}
  • Thanks, but how can I get all records from my table? When I ran my query I only got the first record from the table

  • @Guilhermebartasson, response edited

  • @Guilhermebartasson, as well todos? Your query has a clause WHERE which only returns records that satisfy it

  • It is that I have but of a record with the same date (the condition I am using), but I have already been able to go through the results of the query and save them in an array

  • @Guilhermebartasson, the while will return all who satisfy the calusula where!

  • understood, only one more thing is possible to pass an array as the result of ajax request?

  • json_encode($array)

Show 2 more comments

Browser other questions tagged

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