Javascript 获得;无法读取属性';日期字段';“未定义”的定义;尝试使用mysql表和PHP绘制google图表时

Javascript 获得;无法读取属性';日期字段';“未定义”的定义;尝试使用mysql表和PHP绘制google图表时,javascript,php,charts,google-visualization,Javascript,Php,Charts,Google Visualization,我尝试用PHP从数据库表中检索的值创建google图表,我的数据库连接起来了,但出现了一个错误,如“无法读取未定义的属性'datefield'” 我的数据库表包含两列“行标签(VARCHAR)”,“行中的No之和(INT)”。我不明白我为什么会犯这样的错误 <?php $connection = mysqli_connect('localhost', 'root', '', 'testingoforiginal'); $result = mysqli_query($connection,

我尝试用PHP从数据库表中检索的值创建google图表,我的数据库连接起来了,但出现了一个错误,如“无法读取未定义的属性'datefield'”

我的数据库表包含两列“行标签(VARCHAR)”,“行中的No之和(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>

load('current',{'packages':['bar']});
google.charts.setOnLoadCallback(drawStuff);
函数drawStuff(){
var data=new google.visualization.arrayToDataTable([
['Row Labels','No in Rows'之和],
]);
变量选项={
标题:"BA",,
宽度:900,
图例:{位置:'无'},
图表:{标题:“BA”,
副标题:'},
条形图:“水平”,//物料条形图为必填项。
轴线:{
x:{
0:{side:'top',label:'Sum of No in Rows'}//顶部x轴。
}
},
条:{groupWidth:“90%”
};
var chart=new google.charts.Bar(document.getElementById('top_x_div');
图表绘制(数据、选项);
};

尝试删除第二行值中的额外括号

改变

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


编辑

建议使用
json\u 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); ?>);
var data=new google.visualization.arrayToDataTable();

no@Dharman,我也试过了,但没用,所以我只是参考了一些视频,你看过右边所有相关的帖子了吗。检查他们是否能为你提供解决方案。这不是@Dharmani表现愚蠢的原因,我会检查的。非常感谢你这比我建议的好多少?如果OP使用了
json\u encode
,而不是手动准备数据,那么他们可能会避免这个问题,不是吗?不会说这是更好的,只是找到解决方案的最快方法。我总是建议使用
json\u encode
$.ajax
来获取数据。但大多数情况下,这些建议都没有得到重视……可能是因为堆栈溢出上的人展示了快速而肮脏的方式,而没有展示如何正确地完成它……至少他们让它工作起来了,我真的只是好奇是否额外的括号是问题所在@sreesha请参见上面的编辑。。。
var data = new google.visualization.arrayToDataTable(<?php echo json_encode($data, JSON_NUMERIC_CHECK); ?>);