Adding MySQL + PHP Fields

1

I have the following code in which I need to add the value of a column (MySQL) and the result of this sum, subtract by any value, example: 5000 and the result of this operation be passed to the PHP page. p>

My code:

<?php

$opcoes2 = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8');
$conexao2 = new PDO("mysql:host=".SERVER."; dbname=".DBNAME, USER, PASSWORD,          $opcoes2);

$sql2  = "SELECT SUM(valor_boleto) - 5000 FROM boleto";
$stm2 = $conexao2->prepare($sql2);
$stm2->execute();

while($row2 = $stm2->fetch())  {
$soma = $row2['sum(valor_boleto)'];
 }  

?>

HTML:

<html>
  <body>
   A Soma dos Números do Banco de Dados foi <?=$soma?>.

    <div id="chart_div"></div>
   </body>
 </html>

The error generated is:

  

Notice: Undefined index: sum (ticket_value) in C: \ xampp \ htdocs \ grafico \ grafico.php on line 77

The select is OK, because via console returns the right value.

If my column " valor_boleto " contains 3 fields of 5000 , it would add 15000 and subtract by 5000 , it should return a " echo " of 10000 .

Thank you so much for helping.

    
asked by anonymous 06.09.2016 / 00:43

1 answer

4

Groups the operation and resolves an alias.

SELECT (SUM(valor_boleto)-5000) AS total FROM boleto

Ai la no php gets the total like this:

$soma = $row2['total'];
    
06.09.2016 / 00:50