title: PHP loop sum from database
body: <pre><code>$st = $this-&gt;db-&gt;prepare("SELECT * FROM invoices WHERE group_id=?");
  $st-&gt;execute(array($id));
  
      if($st-&gt;rowCount() &gt;= 1){
          foreach ($st as $row) {
              $counter = $row["paymentAmount"];
              $start = 1;
  
              for($start; $start &lt; $st-&gt;rowCount(); $start++) {
                  $counter = $counter + $row["paymentAmount"];
                  }
          }
  </code></pre>
  
  <p>It actually print out <code>$row["paymentAmount"] + $row["paymentAmount"]</code> and so on, depending on how many <code>$row["paymentAmount"]</code> there is. But the problem is that the last output from <code>$row["paymentAmount"]</code> is 2500.</p>
  
  <p>There is:
  10000
  10000
  2500</p>
  
  <p>And the result is: 7500
  I want it to be: 22500</p>
  
  <p>And if the last result is 3000 it shall be 23000. So what I simply need is this code to take every row from the database, just not the latest one.</p>
  
  <p>Edit: I want it outside of the SQL query</p>