2
Good afternoon everyone, I’m creating a class that will work as a box
where it will sum all the records found in the bank with the column in the value of INPUT and OUTPUT and at the end I subtract Input - Output but it’s not working someone can tell me where I might be missing?
Function of class Lancamento
public function getSaldo(){
$conexao = new Conexao();
$sql1 = "SELECT SUM(valor) FROM $this->table WHERE tipo = Entrada";
$sql2 = "SELECT SUM(valor) FROM $this->table WHERE tipo = Saida";
$entrada = $conexao->login($sql1);
$saida = $conexao->login($sql2);
$saldo = $entrada[0] - $saida[0];
return $saldo;
}
Login function
public function login($rawQuery, $params = array()):array
{
$stmt = $this->query($rawQuery, $params);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
Page of function tests
<?php
require_once "../Controllers/Config.php";
$lancamento = new Lancamento;
$lancamento->getSaldo();
?>
The login class I am using temporarily I intend to implement a specific function for this case, I appreciate any help
Wouldn’t that be:
$saldo = $entrada[0]["SUM(valor)"] - $saida[0]["SUM(valor)"];
? Return any error? If yes, which?– Roberto de Campos
Hello Roberto put as Voce spoke and gave the error below Notice: Undefined offset: 0 in C: xampp htdocs PC gentelella-master Production Class Lancamento.php on line 96 Notice: Undefined offset: 0 in C: xampp htdocs PC gentelella-master Production Class Lancamento.php on line 96
– Matheus Mancini
Your mistake is both
SELECT
, text must be between'
(single quotes).$sql1 = "SELECT SUM(valor) FROM $this->table WHERE tipo = 'Entrada'";
 $sql2 = "SELECT SUM(valor) FROM $this->table WHERE tipo = 'Saida'";
– Roberto de Campos
Unfortunately the error persists Notice: Undefined index: SUM(value) in C: xampp htdocs PC gentelella-master Production Class Lancamento.php on line 96 Notice: Undefined offset: 0 in C: xampp htdocs PC gentelella-master Production Class Lancamento.php on line 96
– Matheus Mancini
In place of
$this->table
you have to put your table name.– Roberto de Campos
This error you are telling me, means that there is no item 0 in the array
$entrada
. It means that yourSELECT
is not returning anything or is wrong.– Roberto de Campos
Roberto, just so you understand in the class there is the attribute table = "Lancamentos" that within this attribute is contained the name of the table understand? funny that this is exactly it, I’m not realizing where I’m going wrong in this select
– Matheus Mancini
It worked Roberto thanks for the man force
– Matheus Mancini