4

使用 PHP 我想从 credit 列中减去 used 列的每个值。我在 phpmyadmin 中创建了一个 tableA。

剩余:信用使用我无法获得结果,我只是获得相同的信用值。

// 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>

我正在尝试执行此查询以获得剩余:

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

谁能给我提示或突出显示错误。比你提前很多。

4

2 回答 2

16

你所拥有的应该可以工作,但为什么不让 MySQL 进行计算呢?

mysql_query("SELECT *, `credit`-`used` AS `remaining` FROM `tableA`");
于 2013-05-16T20:38:18.673 回答
1

你的 php 上有一个右括号

<td> <?php echo $row["credit"] - $row["used"]; ?> </td>
于 2013-05-16T20:38:51.093 回答