微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

尝试使用mysql表和PHP绘制Google图表时,获取“无法读取未定义的属性'datefield'的未定义”

如何解决尝试使用mysql表和PHP绘制Google图表时,获取“无法读取未定义的属性'datefield'的未定义”

我尝试使用使用PHP数据库表中检索的值创建google图表,我的数据库已连接,但出现类似“无法读取未定义的属性'datefield'的错误

我的数据库表包含两列“行标签(VARCHAR)”,“行数之和(INT)” 。我不明白为什么会收到该错误

<?PHP
$connection = MysqLi_connect('localhost','root','','testingoforiginal');
$result = MysqLi_query($connection,"SELECT * FROM ba");
if($result){
   echo "CONNECTED";
}
?>
<html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current',{'packages':['bar']});
      google.charts.setonLoadCallback(drawStuff);

      function drawStuff() {
        var data = new google.visualization.arrayToDataTable([
          ['Row Labels','Sum of No in Rows'],<?PHP
              if(MysqLi_num_rows($result) > 0 ){
                  while($row = MysqLi_fetch_array($result)){
                      echo"['".$row['Row Labels']."',[".$row['Sum of No in Rows']."]],";

                  }
              }
          ?>
        ]);

        var options = {
          title: 'BA',width: 900,legend: { position: 'none' },chart: { title: 'BA',subtitle: ' ' },bars: 'horizontal',// required for Material Bar Charts.
          axes: {
            x: {
              0: { side: 'top',label: 'Sum of No in Rows'} // Top x-axis.
            }
          },bar: { groupWidth: "90%" }
        };

        var chart = new google.charts.Bar(document.getElementById('top_x_div'));
        chart.draw(data,options);
      };
    </script>
  </head>
  <body>
    <div id="top_x_div" style="width: 900px; height: 500px;"></div>
  </body>
</html>

解决方法

尝试删除第二行值中的多余括号...

更改...

echo"['".$row['Row Labels']."',[".$row['Sum of No in Rows']."]],";

到...

echo"['".$row['Row Labels']."',".$row['Sum of No in Rows']."],";

编辑

建议使用json_encode从php传递json数据。

在php中建立数组...

<?php
  $connection = mysqli_connect('localhost','root','','testingoforiginal');
  $result = mysqli_query($connection,"SELECT * FROM ba");
  if($result){
    echo "CONNECTED";
  }

  $data = array();
  $data[] = ['Row Labels','Sum of No in Rows'];
  if(mysqli_num_rows($result) > 0 ){
    while($row = mysqli_fetch_array($result)) {
      $data[] = [$row['Row Labels'],$row['Sum of No in Rows']];
    }
  }
?>

然后在页面上对其进行编码以创建数据表...

var data = new google.visualization.arrayToDataTable(<?php echo json_encode($data,JSON_NUMERIC_CHECK); ?>);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。