Error when displaying the sum in a SELECT

Asked

Viewed 64 times

-3

I’m not getting the total amount.

View

inserir a descrição da imagem aqui

Bench

inserir a descrição da imagem aqui

Values that are in the bank:

R$ 70,88
R$ 70,88
R$ 70,88

Value that appears

R$ 210

Value that has to appear

R$ 212,64

Code

<?php
     $numerocontrato = trim($_GET["numerocontrato"]);
     $consulta = $DB->query("SELECT sum(valordependente) as dep FROM cadastro_clientes where numerocontrato = $numerocontrato");
     while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)) {
            echo "<h1>" . $linha['dep'] . "</h1>";
     } 
?>
  • You don’t have to group -> SELECT sum(valordependente) as dep, adesao FROM cadastro_clientes where numerocontrato = $numerocontrato GROUP BY adesao

  • I’ve already done it @Virgilionovic

  • Ask the question a copy of the data an image anything!

  • 1

    Ready @Virgilionovic the image.

  • So I had to see the data in the database table that there helps it is a result of some list that you did including it added really gives the value that you need. Database values would be ideal.

  • Look there, buddy, @Virgilionovic

  • is saving as text ???

  • Yes, I’m saving as text @Virgilionovic, as I proceed?

  • i put an answer please do the test, have to use 2 functions because of this.

  • @Maurosantos I wasn’t going to comment again to let Virgilio help you, but seeing that he’s helping in the wrong way I have to get in the middle, the way correct is to go to the database and change the type of the dependent value column to DOUBLE, as it is a suitable type to link with decimal values. See you.

  • Thanks @lvcs I will change this, I will do the tests here and do the right one. Correct is Double (10,2)?

  • @Ivcs I will report you can edit a reply yesterday in order to favor you excuse since you are making me this way I will also follow your steps.

  • @Virgilionovic what answer? When editing is intended to correct something, and please, in matters that are not the question comment on chat. PS: Nothing against you, I just don’t think this is the place to go.

  • 1

    @Ivcs already did it regardless of what you think I wouldn’t do that way either, now if you have to learn that also has glass mirror I’ve already signaled what you did wrong yesterday in a PHP question, which is very serious to me. I warned him that the best is decimal, but, this is not for me to solve he has to do in his system, I have nothing against anyone either, but, receiving votes without deserving also find invalid. I’m not the one doing it wrong.

Show 9 more comments

1 answer

0


The correct is for you to record these values in decimal so that you don’t keep converting the data this way below:

<?php
   $numerocontrato = trim($_GET["numerocontrato"]);
   $sql =" SELECT sum(cast(replace(valordependente,',','.') as decimal(18,2))) as dep ";
   $sql .= " FROM cadastro_clientes where numerocontrato = $numerocontrato ";
   $consulta = $DB->query($sql);
   while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)) {
        echo "<h1>" . $linha['dep'] . "</h1>";
   } 

It works, but if this is done in a table where the results are many can cause low performance.

  • -1 because it is right to write the value in the database as DOUBLE or FLOAT

  • Parse error: syntax error, Unexpected '$query' (T_VARIABLE)

  • @Ivcs I know that I was going to tell him but, this is not my fault

  • @Maurosantos missed a dot and comma.

  • @Virgilionovic the duty of the community is to guide the other member to a correct path, not to leave him on the wrong path thinking he is right.

  • Appeared 212.64 can I change this (dot) by comma where? @Virgilionovic

  • @Ivcs I know very well I was still answering the question. I see no reason for it. You understand that or not?

  • @Maurosantos has to format with number_format of PHP

  • 1

    Thank you @Virgilionovic

Show 4 more comments

Browser other questions tagged

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