Trevor Ackermann
Trevor Ackermann

Reputation: 176

Sum of MYSQL Query

I have a MySQL DB

I would like to get the sum or totals of values in my columns I need to echo it to be used in Google Graphs

This is my current code

 <?php 
        $query = "SELECT * from activation";

         $exec = mysqli_query($con,$query);
         while($row = mysqli_fetch_array($exec)){

         echo "['".$row['type']."',".$sum['type']."],"; 
         }
         ?> 

The row Type is the ones I want to get SUM on their values differ from HIJACKING ACCIDENT and so forth values are constant.

The $sum I know is wrong but this is where I need to echo the totals of row type

The following code worked

 <?php 
            $query = "SELECT type, count(type) from activation group by type";

             $exec = mysqli_query($con,$query);
             while($row = mysqli_fetch_array($exec)){

             echo "['".$row['type']."',".$row['count(type)']."],";
             }
             ?> 

Upvotes: 1

Views: 69

Answers (1)

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38552

I guess this is the more precise code that you should use. I used alias of your count(type) as act_type. Hope this helps :)

<?php 
    $query = "SELECT type, count(type) as act_type from activation group by type";
    $exec = mysqli_query($con,$query);
    $expected = [];
    while($row = mysqli_fetch_array($exec)){
         $expected[$row['type']] = $row['act_type'];
         echo "['".$row['type']."',".$row['act_type']."],"; // you can use this line just for printing purpose
   }
   // use $expected array as you wish any where in your code, it contains your result as key=>value manner
?> 

Upvotes: 1

Related Questions