-3

mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given -

$query = "SELECT type, SUM(price) FROM products GROUP BY type";

$result=mysqli_query($db, $query);


while($row = mysqli_fetch_array($result)){
    echo "Total ". $row['type']. " = $". $row['SUM(price)'];

}
devpro
  • 16,074
  • 3
  • 26
  • 39

2 Answers2

1

First of all, check $db identifier, where you define your connection? if connection is fine, then you need to check query error by using mysqli_error()

You should check the $result parameter:

$query = "SELECT type, SUM(price) FROM products GROUP BY type";

$result=mysqli_query($db, $query);

if($result === FALSE) { 
    printf("Error: %s\n", mysqli_error($db)); // TODO: better error handling
}    

while($row = mysqli_fetch_array($result)){
    echo "Total ". $row['type']. " = $". $row['SUM(price)'];

}
devpro
  • 16,074
  • 3
  • 26
  • 39
zvi
  • 3,032
  • 1
  • 23
  • 40
-1

use "as" :

$query = "SELECT type, SUM(price) as sums FROM products GROUP BY type";
$result=mysqli_query($db, $query);
while($row = mysqli_fetch_array($result))
{
echo "Total ". $row['type']. " = $". $row['sums'];
}
esnkrimi
  • 138
  • 1
  • 8