user2391048
user2391048

Reputation:

how to subtract two columns output in a third column using PHP

Using PHP I want to subtract each values of used column from credit column. I have created a tableA in phpmyadmin.

remaining:credit-used I can not get the results i just get same values of credit.

// The desire output in table A

id |date      |credit |used|remaining|
1  |20-5-2013 |400    |300 |100      |
2  |19-5-2013 |300    |100 |200      |
3  |18-5-2013 |600    |50  |550      |  
// db connection  
query select all from tableA
    $sql = "SELECT * FROM tableA";
    $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute           SQL query" '.$sql);
?>

fetch the information and output into a table:

<table >
<tr>
bla bla..
</tr>

// echo:

<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["date"]; ?></td>
<td><?php echo $row["credit"]; ?></td>
<td><?php echo $row["used"]; ?></td>

I'm trying to do this query to get remaining:

// <td> <?php> echo $total= $row["credit"] - $row["used"];?> 
</td>  
</tr>
</table>

can anyone give me a hints or highlight the error. Than you very much in advance.

Upvotes: 4

Views: 29557

Answers (2)

aynber
aynber

Reputation: 23001

You have a closing bracket on your php

<td> <?php echo $row["credit"] - $row["used"]; ?> </td>

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324720

What you have should work, but why not let MySQL do the calculation?

mysql_query("SELECT *, `credit`-`used` AS `remaining` FROM `tableA`");

Upvotes: 16

Related Questions