How to return the total value of items in a table?

Asked

Viewed 182 times

-3

I want to know how to return the total value of items, I use the mysqli to connect to the bank.

Table ec_despesas have the following fields: (more details of the structure in this image)

id, aba, status, nome, categoria, conta, valor, data
  • 3

    It’s simpler you run one desc nome_tabela and put its structure as text instead of an image.

  • Have you tried anything? if you have put the source code, it is total of table items or total of items for a 'category'?

1 answer

4


How are you trying to fetch? Here’s a simple code from a COUNT(*) using Mysqli.

$db = new mysqli('localhost', 'usuario', 'senha', 'easycontrol');

if ($db->connect_errno > 0){
    die('Impossível se conectar ao banco [' . $db->connect_error . ']');
}

$result = $db->query("select count(*) from ec_despesas");
$row = $result->fetch_row();
echo 'n: ', $row[0];
  • In the browser it says: Notice: Undefined variable: db in C: wamp www index.php on line 139

  • and also Fatal error: Call to a Member Function query() on a non-object in C: wamp www index.php on line 139, thanks for the help, to need now at the beginning

  • Before trying to make a query in the bank you need to establish the connection, I will edit my reply with the full code.

  • I got a result " N: 3 ". That’s how many expenses I have, but I want the total sum of the amounts; "of these expenses in cash"

  • 1

    Instead of Count(*), I used SUM(value) and got the result I wanted at this time. Thank you so much for the help! I got it thanks to you!

  • @Deyvischarles In Sopt you are welcome to improve an answer, but changing this code section changes the whole sense of the answer (not to mention that you have not edited the text, only the code). If you want a sum of a field, please edit your question and let us know here in the answer. Why is this not what the question or the original answer imply.

Show 1 more comment

Browser other questions tagged

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