如何回显 HTML 中来自 mysqli_multi_query 的 php 中的值

How to echo values in HTML that are coming from mysqli_multi_query in php

我可以在使用 mysqli_multi_query 时从数据库中获取显示的值。但是我无法在我的 HTML [=18= 中打印] 可能是 while 循环的问题。下面是我的代码,它为我提供了三个 SQL 语句的 AVG。唯一的问题是在 HTML.

中显示平均值
<?php
    $con = new mysqli("localhost", "root","","survey");
    if ($con){ printf(""); }

    $query  = "SELECT AVG(workarea) FROM score;" ;
    $query .= "SELECT AVG(manager) FROM score;";
    $query .= "SELECT AVG(comm) FROM score;";

    // Execute multi query
    if (mysqli_multi_query($con,$query)) {
        do {
            if ($result=mysqli_store_result($con)) {
                // Fetch one and one row
                while ($row=mysqli_fetch_row($result)) {
                    printf("%s\n",$row[0]);
                }

                // Free result set
                mysqli_free_result($result);
            }
        } while (mysqli_next_result($con));
    }
?>
<div class="row count">
WORK AREA/UNIT SCORE : <?php echo $row[0]; ?> // echo $row[AVG(workarea)]  doesnot work
</div>

<div class="row count">
SUPERVISOR/MANAGER SCORE : <?php echo $row[0]; ?> // echo $row[AVG(manager)]     
</div>

<div class="row count">
COMMUNICATION SCORE : <?php echo $row[0]; ?> // echo $row[AVG(comm)] doesnot work
</div>

无需多次查询,您只需在一次查询中获得 AVG。并在 while 循环中将你的 html 写为

 $con = new mysqli("localhost", "root", "", "survey");
if ($con) {
    printf("");
}
$query = "SELECT AVG(workarea),AVG(manager),AVG(comm) FROM score;";
// Execute multi query
if ($result = $con->query($query)) {

    /* fetch object array */
    while ($row = $result->fetch_row()) {
        ?>
        <div class="row count">
            WORK AREA/UNIT SCORE : <?php echo $row[0]; ?> 
        </div>

        <div class="row count">
            SUPERVISOR/MANAGER SCORE : <?php echo $row[1]; ?>    
        </div>

        <div class="row count">
            COMMUNICATION SCORE : <?php echo $row[2]; ?> 
        </div>
        <?php
    }

    /* free result set */
    $result->close();
}