MySql Group By and Sum total value of other column and then Display in PHP MySQLi Fetch Array

707 Views Asked by At

In theory, I have two columns like so: enter image description here

I want the result to be dog 6, and Elephant 2. What I have made so far (and failed [the amount does not get displayed]) is this:

$query_check_credentials = "SELECT word, SUM(amount) FROM Data Group By word";


$result = mysqli_query($dbc, $query_check_credentials);

while($row = mysqli_fetch_array( $result ))
            {
                echo $row['amount'];
}
1

There are 1 best solutions below

0
On BEST ANSWER

You should give an alias to the sum:

SELECT word, SUM(amount) AS total_amount FROM Data GROUP BY word

and then use $row['total_amount'].

If you don't do that, you have to use $row['SUM(amount)']