php 如何使用PHP在第三列中减去两列输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16597000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:24:49  来源:igfitidea点击:

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

phpmysqlphpmyadmin

提问by Jaime A

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

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

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.

任何人都可以给我提示或突出显示错误。比你提前很多。

采纳答案by aynber

You have a closing bracket on your php

你的 php 上有一个右括号

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

回答by Niet the Dark Absol

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

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

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