1

我想自动计算销售额、费用和银行存款的总额

<?php

$result = mysql_query("select * from new");

$x = 0;
while($row = mysql_fetch_assoc($result))
{
?>

<tr>
    <td><?php echo $row["D_date"]; ?></td>
    <td><?php echo $row["D_id"]; ?></td>
    <td><?php echo $row["D_item"]; ?></td>
    <td><?php echo $row["D_sales"]; ?></td>
    <td><?php echo $row["D_expenses"]; ?></td>
    <td><?php echo $row["D_bankin"]; ?></td>
</tr>

<?php
$x++;
}
?>

<?php

$result1 = mysql_query("select * from new");
while($row1 = mysql_fetch_assoc($result1))
{
?>
    <tr>
        <td colspan="3">Total</td>
        <td><?php echo $row1["D_totalsales"]; ?> </td>
        <td><?php echo $row1["D_totalexpenses"]; ?></td>
        <td><?php echo $row1["D_totalbankin"]; ?></td>
    </tr>
<?php
}
?>

谢谢你

4

3 回答 3

1

无需为此触发两个查询。您可以在单个查询中执行相同的操作。

将您的查询更改为

$results = mysql_query("SELECT *,SUM(`D_sales`) as D_totalsales ,SUM(`D_expenses`) as D_totalexpenses ,SUM(`D_totalbankin`) as D_totalbankin FROM new");

while($row = mysql_fetch_assoc($result))
{
?>
<tr>
  <td><?php echo $row["D_date"]; ?></td>
  <td><?php echo $row["D_id"]; ?></td>
  <td><?php echo $row["D_item"]; ?></td>
  <td><?php echo $row["D_sales"]; ?></td>
  <td><?php echo $row["D_expenses"]; ?></td>
  <td><?php echo $row["D_bankin"]; ?></td>
  <td><?php echo $row["D_totalsales"]; ?></td>
  <td><?php echo $row["D_totalexpenses"]; ?></td>
  <td><?php echo $row["D_totalbankin"]; ?></td>
</tr> 
?>
于 2013-02-25T08:04:02.903 回答
0
 SELECT SUM(D_sales), ... FROM table_name
于 2013-02-25T08:01:36.030 回答
-1

您可以使用 mysql_num_rows()

echo mysql_num_rows($result);
于 2013-02-25T07:57:37.027 回答