0

I am able to get values displayed from database when using mysqli_multi_query.But I am not able to print in my HTML code.Its Probably the issue with while loop. Below is my code which gives me the AVG for the three SQL statements. Only problem is with displaying average value in 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>
Saty
  • 22,443
  • 7
  • 33
  • 51
Dev
  • 69
  • 2
  • 10

1 Answers1

2

No need of multi query you just get AVG in single query. And write your html inside while loop as

 $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();
}
Saty
  • 22,443
  • 7
  • 33
  • 51