0
Hello
I want to add the total values of the result of a PDO query. 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/systems/webApps/fluxo_de_box/fluxojoin_2.0/php/reportPorDiaTValorEntrates.php online 106
Line 106, entered in the above message is: $valueTotalEntrates = $valueTotalEntrates + $value;
And all my 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 go to do the sum, via SQL itself, how should I get the result? I have to create an alias for the "SUM(value)"?
You are using
$valorTotalEntradas
for both the statement Pdo and the sum. Use another variable for the sum.– bfavaretto
Or make a direct
SELECT SUM(valor) FROM ...
.– bfavaretto
But how to recover the value of that SELECT SUM(value) FROM...?
– GustavoSevero
@Gustavosevero does
SELECT SUM(Valor) AS SomaValor FROM
and then just do$valor = $linha['SomaValor']
– Diéfani Favareto Piovezan