Add result from a column and display

Asked

Viewed 42 times

-2

all good? I am making an entry in the database in a certain column. However, when I try to use the WHILE function to show me the result, instead of it adding up it brings me all the results. However, I want to add up the result and display the final sum. See below the code I’m using.

<?php 

            $Fundos = mysqli_query($conexao, "SELECT * FROM saldo WHERE login = '$usuario'");

            while($exibeFundos = mysqli_fetch_assoc($Fundos)){

            $valor = $exibeFundos['valorSaldoAtivo'] + $exibeFundos['valorSaldoAtivo'];
?>
            <?=$valor; ?>

            <?php }      ?>
  • 1

    "making a seat insert", insertion or is just selecting?

1 answer

-1

I managed to resolve. thanks @Rodolfo Silva, but I got vlw.

<?php 

            $Fundos = mysqli_query($conexao, "SELECT * FROM saldo WHERE login = '$usuario'");

                        $valor = 0;

            while($exibeFundos = mysqli_fetch_array($Fundos)){

            $valor = $valor + $exibeFundos['valorSaldoAtivo'];
 }  
    ?>

 <?=$valor?> 
  • 1

    There is no reason to do this in PHP. In SQL you can do it SELECT SUM(valorSaldoAtivo) as total and return the result at once.

  • Wow, I didn’t know....

Browser other questions tagged

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