Php Mysql Display sum of 2 columns in html -


i want display result of "vendido -pagado" (2 columns of table) in html following code doesn't work, clue why?

 <?php     //connect db     include '../connect.php';      $saldo = "select vendido-pagado saldo cooks mail = '$email'";     $resultsaldo = mysqli_query($conn, $saldo);        if (mysqli_num_rows($resultsaldo) > 0) {         // output data of each row                 while($row3 = mysqli_fetch_assoc($resultsaldo) {                   echo  '<a class="w-nav-link navlink right" href="../misaldo.php">saldo: '   .$row3['saldo']. "</a>";                    }}          else { echo "there error fetching total";}      mysqli_close($conn);     ?> 

for sum in mysql query, can use mysql sum aggregate function

$saldo = "select (sum(vendido) + sum(pagado)) saldo cooks mail = '$email'"; 

or

i have used $row3['col1'] , $row3['col2'] showing how sum 2 columns not forget change column's name

col1 , col2.

while($row3 = mysqli_fetch_assoc($resultsaldo) {  echo "sum of 2 columns : ".($row3['col1'] + $row3['col2']);  } 

note: if using mysqli better use mysqli prepared statements.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -