【问题标题】:PHP get sum of SQL tablePHP获取SQL表的总和
【发布时间】:2013-05-13 23:17:05
【问题描述】:

我有 MAMP(本地托管 SQL、WEB 等服务器),数据库名称是:NKTDEBITS,表名称是:Insurance,表上的列是 STATECOV。我知道我对此很接近,但仍然在应该产生总数的领域中得到一个黑色,有人知道吗?

<?php
    $con=mysqli_connect("localhost","root","root","KNTDebits");
    // Check connection
    if (mysqli_connect_errno())
      {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      }

    $result = mysqli_query($con,"SELECT * FROM Insurance");
    $result2 = mysqli_query($con,"SELECT * FROM Office");
    $result3 = mysqli_query($con,"SELECT * FROM RichmondLocation");
    $result4 = mysqli_query($con,"SELECT * FROM DanvilleLocation");
    $result5 = mysql_query('SELECT SUM(STATECOV) AS STATECOV_sum FROM Insurance'); 


    echo "<table border='1'>
    <tr>
    <th>Truck Number</th>
    <th>VIN</th>
    <th>Make</th>
    <th>Model</th>
    <th>State Coverage</th>
    <th>Comprehinsive Coverage</th>
    <th>Property Damage/th>
    <th>Personal Injury</th>
    </tr>";

    while($row = mysqli_fetch_array($result))
      {
      echo "<tr>";
      echo "<td>" . $row['TNUM'] . "</td>";
      echo "<td>" . $row['VIN'] . "</td>";
      echo "<td>" . $row['MAKE'] . "</td>";
      echo "<td>" . $row['MODEL'] . "</td>";
      echo "<td>" . $row['STATECOV'] . "</td>";
      echo "<td>" . $row['COMPRE'] . "</td>";
      echo "<td>" . $row['PROPDMG'] . "</td>";
      echo "<td>" . $row['PRSINJ'] . "</td>";
      echo "</tr>";
      }
    echo "</table>";


    //Table 2 Start
    str_repeat('&nbsp;', 5); // adds 5 spaces
    echo "<table border='5'>

    <tr>
    <th>Richmond</th>
    <th>Date</th>
    <th>Payment</th>
    <th>Payer</th>
    </tr>";

    while($row3 = mysqli_fetch_array($result3))
     {
      echo "<tr>";
      echo "<td>" . $row3[''] . "</td>";
      echo "<td>" . $row3['DATE'] . "</td>";
      echo "<td>" . $row3['PAYMENT'] . "</td>";
      echo "<td>" . $row3['PAYER'] . "</td>";
      echo "</tr>";
      }
    echo "</table>";

    //Table 4 Start
    str_repeat('&nbsp;', 5); // adds 5 spaces
    echo "<table border='5'>

    <tr>
    <th>Danville</th>
    <th>Date</th>
    <th>Payment</th>
    <th>Payer</th>
    </tr>";

    while($row4 = mysqli_fetch_array($result4))
      {
      echo "<tr>";
      echo "<td>" . $row4[''] . "</td>";
      echo "<td>" . $row4['DATE'] . "</td>";
      echo "<td>" . $row4['PAYMENT'] . "</td>";
      echo "<td>" . $sum . "</td>";
      echo "</tr>";
      }
    echo "</table>";

    //Table 5 Start

    echo "<table border='5'>

    <tr>
    <th>Total</th>
    </tr>";

    $result = mysql_query('SELECT SUM(STATECOV) AS value_sum FROM Insurance'); 
    $row = mysql_fetch_assoc($result); 
    $sum = $row['value_sum'];

    while($row = mysql_fetch_assoc($result));

      {
      echo "<tr>";
      echo "<td>" . $sum . "</td>";
      echo "</tr>";
      }
    echo "</table>";


    mysqli_close($con);
?> 

【问题讨论】:

  • 您没有将连接句柄传递给 mysql_query。您还应该使用 mysqli / pdo。
  • 去掉表 5 中的 while 循环 - 它总是会被跳过,因为您已经阅读了 ionly 行。
  • 不要混搭 MySQL 和 MySQLi
  • 我不知道我在使用 MYSQLi,因为这是从我研究的其他人那里获取的。如果我摆脱了表 5 的循环,是否会扼杀获得列总和的任何可能性?

标签: php sql database mysqli


【解决方案1】:

您需要一个 GROUP BY 才能与您的 SUM 一起使用。我对您的表格了解得不够多,无法让您知道应该为此使用哪一列。

您应该在查询数据库时处理潜在的错误情况,因为当您遇到数据库/查询错误时,您会很快看到。

您还应该考虑使用mysqliPDO 而不是已弃用的mysql_* 函数。

【讨论】:

  • 怎么样(杀死表 5 中的循环) $result = mysql_query('SELECT SUM(STATECOV) AS value_sum FROM Insurance'); //$row = mysql_fetch_assoc($result); $sum = $row['value_sum'];而($row = mysql_fetch_assoc($result)); { 回声“”;回声“”。总和。 "";回声“”; } 回声“”;
  • $result = mysql_query('SELECT SUM(STATECOV) AS value_sum FROM Insurance'); //$row = mysql_fetch_assoc($result); $sum = $row['value_sum'];而($row = mysql_fetch_assoc($result)); { 回声“”;回声“”。总和。 "";回声“”; } 回声“”;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 2011-03-13
  • 2017-12-09
  • 1970-01-01
  • 2021-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多