How to do sum with php or SQL?

0

Hello

I want to sum the total values of the result of a query in PDO. However, when executing the command the following message appears in the console:

  

Object of class PDOStatement could not be converted to int in /Applications/MAMP/htdocs/sistemas/webApps/fluxo_de_deixa/fluxojoin_2.0/php/relatedtoPorDiaTValorEntradas.php on line 106

Line 106, reported in the above message is: $ valueTotalEntrades = $ valueTotalEntrades + $ value;

And my whole code is this:

<?php
ini_set('display_errors', true);
error_reporting(E_ALL);

include_once("con.php");

$pdo = conectar();

$id_empresa = $_GET['id_empresa'];
$data = $_GET['data'];

$tipo = "SAI";

$valorTotalEntradas=$pdo->prepare('SELECT valor FROM importa
                                                        WHERE data=:data
                                                        AND 
                                                      id_empresa=:id_empresa
                                                        AND tipo=:tipo');

$valorTotalEntradas->bindValue('id_empresa', $id_empresa);
$valorTotalEntradas->bindValue('data', $data);
$valorTotalEntradas->bindValue('tipo', $tipo);
$valorTotalEntradas->execute();

while ($linha=$valorTotalEntradas->fetch(PDO::FETCH_ASSOC)) {

    $valor = $linha['valor'];
    $valorTotalEntradas = $valorTotalEntradas + $valor;

    $valorTotalEntradas = number_format($valorTotalEntradas,2,',','.');

    $return[] = array(
        'valorTotalEntradas' => $valorTotalEntradas
    );

}

echo json_encode($return);
?>

And if I do the sum, via SQL itself, how should I get the result? Do I have to create an alias for the "SUM (value)"?

    
asked by anonymous 26.12.2017 / 19:17

0 answers