dongwen7283 2017-07-11 09:37
浏览 35
已采纳

如何通过mysqli和php中的循环返回平均数据

I am a beginner programmer, and i want to fetch AVG() data in every rows created by the while loop, this is the code,

<?php

$sql = "SELECT sites_id, sites_nama, sites_alamat, sites_kota_kabupaten, perpanjangan_pagu, sites_tanggal_start, sites_tanggal_finish, perpanjangan_invoice, AVG(perpanjangan_pagu) FROM site";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<table>
        <tr>
                        <th>Site ID</th>
                        <th>Site Name</th>
                        <th>Alamat</th>
                        <th>Kab.Kota</th>
                        <th>Pagu</th>
                        <th>Harga Rata Rata</th>
                        <th>Awal Kontrak</th>
                        <th>Akhir Kontrak</th>
                        <th>Invoice</th>
        </tr>";
    // output data of each row
    $rows = array();
    while ($row = $result->fetch_assoc()) {
        $rows = $row["AVG(perpanjangan_pagu)"];
        echo "
            <tr>
                    <td>" . $row["sites_id"] . "</td>
                    <td>" . $row["sites_nama"] . "</td>
                    <td>" . $row["sites_alamat"] . "</td>
                    <td>" . $row["sites_kota_kabupaten"] . "</td>
                    <td>" . $row["perpanjangan_pagu"] . "</td>
                    <td>" . $rows . "</td>
                    <td>" . $row["sites_tanggal_start"] . "</td>
                    <td>" . $row["sites_tanggal_finish"] . "</td>
                    <td>" . $row["perpanjangan_invoice"] . "</td>
            </tr>";
    }

    echo "</table>";
}
else {
    //echo "0 results";
}
$conn->close();
?>

and this is the image, you can see that it only return one row.

the first pic

and this is the image when I deleted the avg function.

the second image

please help me to fetch the average data in every row in the table, not just only one table,

Thanks.

  • 写回答

1条回答 默认 最新

  • doujiu9172 2017-07-12 04:50
    关注

    can you try this I have modified user code & added the AVG() column.

    <?php
    $sql = "SELECT sites_id, sites_nama, sites_alamat, sites_kota_kabupaten, perpanjangan_pagu, sites_tanggal_start, sites_tanggal_finish, perpanjangan_invoice, (SELECT AVG(perpanjangan_pagu) FROM site) AS 'AVG_pagu' FROM site";
    $result = $conn->query($sql);
    //  AVG_pagu has the AVG value of all columns of `perpanjangan_pagu` in table `site`
    if ($result->num_rows > 0) {
        echo "<table>
            <tr>
                            <th>Site ID</th>
                            <th>Site Name</th>
                            <th>Alamat</th>
                            <th>Kab.Kota</th>
                            <th>Pagu</th>
                            <th>Harga Rata Rata</th>
                            <th>Awal Kontrak</th>
                            <th>Akhir Kontrak</th>
                            <th>Invoice</th>
            </tr>";
        //  output data of each row
        //  $rows = array(); // This is not actually required
        while ($row = $result->fetch_assoc()) {
            //$rows[] = $row["AVG_pagu"]; // This is not actually required
            echo "
                <tr>
                        <td>" . $row["sites_id"] . "</td>
                        <td>" . $row["sites_nama"] . "</td>
                        <td>" . $row["sites_alamat"] . "</td>
                        <td>" . $row["sites_kota_kabupaten"] . "</td>
                        <td>" . $row["perpanjangan_pagu"] . "</td>
                        <td>" . $row["AVG_pagu"] . "</td>
                        <td>" . $row["sites_tanggal_start"] . "</td>
                        <td>" . $row["sites_tanggal_finish"] . "</td>
                        <td>" . $row["perpanjangan_invoice"] . "</td>
                </tr>";
        }
    
        echo "</table>";
    }
    else {
        echo "No records found!";
    }
    $conn->close();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)