Method of subtracting two values from the sum of records coming from the Mysql database

Asked

Viewed 238 times

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?

  • 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

  • Your mistake is both SELECT, text must be between ' (single quotes). $sql1 = "SELECT SUM(valor) FROM $this->table WHERE tipo = 'Entrada'";&#xA; $sql2 = "SELECT SUM(valor) FROM $this->table WHERE tipo = 'Saida'";

  • 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

  • In place of $this->table you have to put your table name.

  • This error you are telling me, means that there is no item 0 in the array $entrada. It means that your SELECT is not returning anything or is wrong.

  • 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

  • It worked Roberto thanks for the man force

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.