如何使总数据插入表格?

时间:2013-02-25 07:54:43

标签: php mysql phpmyadmin

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

<?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
}
?>

谢谢

3 个答案:

答案 0 :(得分: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> 
?>

答案 1 :(得分:0)

 SELECT SUM(D_sales), ... FROM table_name

答案 2 :(得分:-1)

您可以使用mysql_num_rows()

echo mysql_num_rows($result);