1

我有 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);
?> 
4

1 回答 1

0

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

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

您还应该考虑使用mysqliorPDO代替已弃用的mysql_*功能。

于 2013-05-13T23:22:13.750 回答