如何创建google图表

3qpi33ja  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(417)

我想让谷歌图表基于我的mysql。在localhost中一切正常。显示时没有错误。而且很管用。
但当我试图上传到我的服务器,它显示什么。
这是我的密码:

<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>

当我想跑的时候 SELECT date, COUNT(id_master_post) FROM master_post GROUP BY DAY(date) ASC 从phpmyadmin输入sql。给我一些错误。
选择日期,按日期(日期)asc limit 0,25 mysql menyatakan:dokumentasi从master\u post组中计数(id\u master\u post)
select list的ţ1055-表达式ţ1不在group by子句中,并且包含未聚合的列“piratefiles.masterţpost.date”,该列在功能上不依赖于group by子句中的列;这与sql\u mode=only\u full\u group by不兼容

gkl3eglg

gkl3eglg1#

使用 query 以及 execute 连续是混乱的,而且很可能是错误的 execute 方法用于执行准备语句,但您没有创建这样的准备语句。我刚开始格式化你的代码,这样我就可以通过它来看看我是否可以识别一个错误,并最终得到以下可能有用,也可能不有用的结果。我把评论放进去帮点忙。

<?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>

测试演示,基于您的 pageview 插入了一些随机示例数据的表。

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

和一段伪数据

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>

相关问题