Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

get average of sql column php

//1 selecting the average from table column
$sql = "SELECT avg(columnName) as whatever FROM tableName";
//2 getting the result
$result = mysqli_query($conn, $sql);
//3 fetch as array
$numratingsa = mysqli_fetch_all($result, MYSQLI_ASSOC);
//4 select last element of array which is average
$myLastElementa = end($numratingsa);
//5 converts array to string
$stringo = implode($myLastElementa);
// *optional* round value to one decimal place
$roundin = round($stringo, 1);
//6 show output
echo $stringo;
echo $roundin;
 
PREVIOUS NEXT
Tagged: #average #sql #column #php
ADD COMMENT
Topic
Name
6+1 =