-2

i want to make google chart based my mysql. in localhost everything ok. no error showing. and it work.

but when i try to upload to my server it showing nothing.

here my code:

    <script>
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {

        var data = google.visualization.arrayToDataTable([
           ['Date', 'Master Posts'],
<?php
$views = $db->query("SELECT date, COUNT(id_master_post) FROM master_post GROUP BY DAY(date) ASC");
$views->execute();
while ($value2 = $views->fetch()) { 
echo "['".substr($value2['date'], 0, -9)."',".$value2['COUNT(id_master_post)']."],";
} ?>
        ]);

        var options = {
          title: 'Master Posts Created',
        };

        var chart = new google.visualization.PieChart(document.getElementById('Views'));

        chart.draw(data, options);
      }
    </script>
  <body>
    <div id="Views" style="width: 500px; height: 500px; float: left;"></div>
  </body>

when i try to run SELECT date, COUNT(id_master_post) FROM master_post GROUP BY DAY(date) ASC stright in sql from phpmyadmin. give me some error.

SELECT date, COUNT(id_master_post) FROM master_post GROUP BY DAY(date) ASC LIMIT 0, 25 MySQL menyatakan: Dokumentasi

'#1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'piratefiles.master_post.date' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
jazuly aja
  • 89
  • 10
  • Can you show us an error message? Please read [this guide](https://stackoverflow.com/help/how-to-ask) on how to ask a question on SO. – creyD Jan 18 '18 at 09:23
  • no error massages. – jazuly aja Jan 18 '18 at 09:23
  • Please isolate where the code begins to fail. Echo some strategically placed checkpoints. – mickmackusa Jan 18 '18 at 09:25
  • if you look at the source code is there a fully populated datatable? – Professor Abronsius Jan 18 '18 at 09:25
  • 1
    The sql query has `where month(date)` but no condition to that - so it will always be true - is that expected? – Professor Abronsius Jan 18 '18 at 09:29
  • @RamRaider im delete it but still no working – jazuly aja Jan 18 '18 at 09:34
  • Post the relevant portion of source code as RamRaider requested. Use backticks on `date` column -> it is a keyword. – mickmackusa Jan 18 '18 at 09:36
  • ok - so without `where month(date)` does the query actually return any data? – Professor Abronsius Jan 18 '18 at 09:40
  • 1
    `$views = $db->query` and `$views->execute();` ?? I think the first ought to be `$views = $db->prepare()` if you are calling `execute()` – Professor Abronsius Jan 18 '18 at 09:43
  • ...but it worked locally? – mickmackusa Jan 18 '18 at 09:44
  • @mickmackusa yes it work in localhost, but not work in my server. i think the proplem is in `SELECT date, COUNT(idpv) ....` i try to run stright in sql phpmyadmin it give me error, but working in sql phpmyadmin local. – jazuly aja Jan 18 '18 at 09:49
  • Do share the error with us. – mickmackusa Jan 18 '18 at 09:50
  • @RamRaider with/without `Where` output is same. – jazuly aja Jan 18 '18 at 09:52
  • Going to have to vote to close as offtopic Why isnt my code working, if you don't provide the necessary details. – mickmackusa Jan 18 '18 at 09:54
  • @mickmackusa im edit my question, hope someone can help me. – jazuly aja Jan 18 '18 at 09:56
  • Possible duplicate of [MySQL - Selecting a Column not in Group By](https://stackoverflow.com/questions/1023347/mysql-selecting-a-column-not-in-group-by) – mickmackusa Jan 18 '18 at 10:08
  • Maybe https://stackoverflow.com/questions/38807570/mysql-5-0-12-list-is-not-in-group-by-clause-and-contains-nonaggregated-column is more informative for you. There are many duplicates to find. You could have spared RamRaider so much trouble if you would have brought the error to your initial post (in fact you may have avoided asking at all if you would have found the error message on your own and continued to research). – mickmackusa Jan 18 '18 at 10:29
  • I suppose now you just need to apply the WHERE clause to target your desired year - month and GROUP BY date (not DAY(date)). – mickmackusa Jan 18 '18 at 10:35
  • ...oh this is a DATETIME column isnt it? So the hh:mm:ss won't group. – mickmackusa Jan 18 '18 at 10:41
  • @mickmackusa i want to group count by day, so result will group in day, if im just using date it will count all of it, i want to showing data by day. so if im posting 20 data in that day it will record 1 data if im using day(date), but if im only using date it will record 20 data – jazuly aja Jan 18 '18 at 10:44
  • can you add your table schema to the question as it is unclear what type of columns are in question here. ie: is `date` a simple `date` column or `datetime` ? – Professor Abronsius Jan 18 '18 at 11:30

1 Answers1

1

The use of query and execute consecutively was confusing and quite possibly wrong - the execute method is used to execute a prepared statement but you did not create such a prepared statement. I began just formatting your code so I could work through it to see if I could identify an error and ended up with the following which might, or might not, be of use. I put comments in to help a little.

<?php

    /* column headers for chart datatable */
    $payload=array( "['Date', 'Post']" );

    /* create sql and prepare statement */
    $sql='select `date`, count(`idpv`) as `count` from `pageview` group by day( `date` ) desc';
    $stmt=$db->prepare( $sql );

    if( $stmt ){
        /* execute sql statement */
        $result=$stmt->execute();
        $stmt->store_result();

        /* If there are results, process recordset */
        if( $result && $stmt->num_rows > 0 ){

            /*  bind results */

            $stmt->bind_result( $date, $count );

            /* fetch recordset */
            while ( $stmt->fetch() ) {

                $date=substr( $date, 0, -9 );
                $payload[]="[ '$date', $count ]";

            }
            $stmt->free_result();
            $stmt->close();
        }       
    }
    $db->close();

    /* 
        if there were no results this will only have column headers for datatable 
        this will be used by javascript function
    */
    $srcdata='['.implode( ',', $payload ).']';
?>

<html>
    <head>
        <meta charset='utf-8' />
        <title>Google charts & mysqli</title>
        <!--

            assumed here that the required google api scripts have been loaded

        -->
        <script>
            google.charts.load('current', {'packages':['corechart']});
            google.charts.setOnLoadCallback( drawChart );

            function drawChart() {
                /* 
                    echo the source data as a javascript variable before using in datatable
                */
                var srcdata=<?php echo $srcdata; ?>

                if( srcdata.length > 1 ){
                    /* 
                        There are more than just headers in src data array
                        so add to datatable and create chart
                    */
                    var data = google.visualization.arrayToDataTable( srcdata );

                    var options = {
                      title: 'Page Views'
                    };

                    var chart = new google.visualization.PieChart( document.getElementById('Views') );
                    chart.draw( data, options );
                }
            }
        </script>
    </head>
    <body>
        <div id="Views" style="width: 500px; height: 500px; float: left;"></div>
    </body>
</html>

Tested demo, based upon a rudimentary emulation of your pageview table with some random example data inserted.

mysql> describe pageview;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| date  | datetime     | NO   |     | NULL    |       |
| idpv  | mediumint(9) | NO   |     | 0       |       |
+-------+--------------+------+-----+---------+-------+

and a snippet of the dummy data

mysql> select * from pageview limit 10;
+---------------------+------+
| date                | idpv |
+---------------------+------+
| 2015-01-11 00:00:00 |   54 |
| 2015-01-17 00:00:00 |   55 |
| 2015-01-20 00:00:00 |  121 |
| 2015-02-07 00:00:00 |   42 |
| 2015-03-08 00:00:00 |   57 |
| 2015-03-09 00:00:00 |  122 |
| 2015-04-01 00:00:00 |    5 |
| 2015-04-12 00:00:00 |   39 |
| 2015-04-19 00:00:00 |   98 |
| 2015-04-20 00:00:00 |   90 |
+---------------------+------+

-

<?php

    include __DIR__ . '/db.php'; #pertinent to my system!


    /* column headers for chart datatable */
    $payload=array( "['Date', 'Post']" );

    /* create sql and prepare statement */
    $sql='select 
            `date`, 
            count(`idpv`) as `count` 
            from `pageview` 
            group by day( `date` )
            order by date( `date` ) desc';

    $stmt=$db->prepare( $sql );
    if( $stmt ){
        /* execute sql statement */
        $result=$stmt->execute();
        $stmt->store_result();
        /* If there are results, process recordset */
        if( $result && $stmt->num_rows > 0 ){

            /* store and bind results */

            $stmt->bind_result( $date, $count );

            /* fetch recordset */
            while ( $stmt->fetch() ) {
                $payload[]="[ '$date', $count ]";
            }
            $stmt->free_result();
            $stmt->close();
        }       
    }
    $db->close();

    /* 
        if there were no results this will only have column headers for datatable 
        this will be used by javascript function
    */
    $srcdata='['.implode( ',', $payload ).']';

?>

<html>
    <head>
        <meta charset='utf-8' />
        <title>Google charts & mysqli</title>
        <!--

            assumed here that the required google api scripts have been loaded

        -->
        <script src='https://www.gstatic.com/charts/loader.js'></script>
        <script>
            google.charts.load('current', {'packages':['corechart']});
            google.charts.setOnLoadCallback( drawChart );

            function drawChart() {
                /* 
                    echo the source data as a javascript variable before using in datatable
                */
                var srcdata=<?php echo $srcdata; ?>

                if( srcdata.length > 1 ){
                    /* 
                        There are more than just headers in src data array
                        so add to datatable and create chart
                    */
                    var data = google.visualization.arrayToDataTable( srcdata );

                    var options = {
                      title: 'Page Views'
                    };

                    var chart = new google.visualization.PieChart( document.getElementById('Views') );
                    chart.draw( data, options );
                }
            }
        </script>
    </head>
    <body>
        <div id="Views" style="width: 500px; height: 500px; float: left;"></div>
    </body>
</html>

enter image description here

Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46