title: Running Balance with PHP and MySQL
body: <p>I am working on a simple finance tracker and appear to be stuck trying to figure out what should be a simple task using PHP and MySQL - displaying a running financial balance.</p>
  
  <p>There are two MySQL tables, one with the starting balance for each account and a second with the individual transactions. For the purpose of development, I am only working with one account. What I am trying to do is iterate through the transactions and display a running total, subtracting from the starting balance if it is a withdraw or adding if it is a deposit. The <code>amount</code> column for each transaction is negatively signed for withdraws and positively for deposits.</p>
  
  <p>Here is what I have tried without any luck:</p>
  
  <pre><code>&lt;?php
    $getTransactions = $db-&gt;query("SELECT * FROM transactions ORDER BY date, id");
    //Get the starting balance
    $getStartingBalance = $db-&gt;query("SELECT amount FROM startingBalance WHERE id = 1");
    $startingBalance = $getStartingBalance-&gt;fetch();
    //Start off with Running Total same as Starting Balance
    $runningTotal= $startingBalance['amount'];
    //Iterate through transactions
    while ($transaction = $getTransactions-&gt;fetch()) {
      $amount = $transaction['amount'];
      if ($amount &lt; 0) {
        $runningTotal = $runningTotal - $amount;
      } else {
        $runningTotal = $runningTotal + $amount;
      }
    }
  ?&gt;
  </code></pre>
  
  <p>The above will perform addition even if the number is negative. How would I accomplish this with signed numbers? In the past, I was using an integer to differentiate between deposits and withdraws (1 for deposit, 2 for withdraw) but it was suggested that signed values would be the better route.</p>
  
  <p>Thanks for any help</p>