Error displaying the sum in a SELECT [closed]

-3

I'm not getting the full amount.

View

  

Bank

  

Valuesthatareinthebank:

R$70,88$70.88

Valuethatappears

  

$210

Valuethathastoappear

  

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>";
     } 
?>
    
asked by anonymous 03.01.2017 / 01:31

1 answer

0

The correct thing is for you to write these values to decimal so that you do not convert the data like this 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 doing so in a table where the results are too many can cause poor performance.

    
03.01.2017 / 01:59