Kelsey
Kelsey

Reputation: 921

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

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'];
}

Upvotes: 1

Views: 693

Answers (1)

Barmar
Barmar

Reputation: 780889

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)']

Upvotes: 3

Related Questions