Sum of MYSQL Query

66
December 30, 2018, at 8:20 PM

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)']."],";
             }
             ?> 
Answer 1

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
?> 
READ ALSO
Is it a good idea to register style only in certain WP pages?

Is it a good idea to register style only in certain WP pages?

I have a page speed issue, the theme I bought is really crappy but i cannot change it nowI use WP_rocket, server have HTTP2 but still it is to many resources to load

49
PHP curl taking so long to execute and in the end it does not do anything on de nodejs server

PHP curl taking so long to execute and in the end it does not do anything on de nodejs server

I'm setting up a new notification server with nodejs and socketio

47
Having trouble adding validation errors to ACF form

Having trouble adding validation errors to ACF form

I'm using ACF with WordPress and I have a form that I am trying to add a validation error to, but it doesn't appear to be working (I think I know why)

99
Save and display order custom meta data in Woocommerce 3+

Save and display order custom meta data in Woocommerce 3+

A while back my partner helped me to add a custom field to our checkout page, we wanted to ask people "How did you hear about us?" when they booked for our events

66