我正在尝试使用谷歌图表来显示我的查询结果。这是php部分:
<?php
include('db.php');
$mysqli = SQLConnect();
$myArray = array();
$sql = "select id, rating from ratings order by RateDate DESC limit 5";
if ($result = $mysqli->query($sql)) {
while($row = $result->fetch_assoc()) {
$myArray[] = $row;
}
echo json_encode($myArray);
}
$result->close();
$mysqli->close();
?>
这是我尝试使用它的实际部分:
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart1);
function drawChart1() {
var options = {
title: 'Last 5 Ratings',
height: 400,
legend: { position: 'bottom' }
};
var jsonData = $.post({
url: '../resources/db/getDataForChart1.php',
dataType:"json",
async: false,
success: function(jsonData){
var data = new google.visualization.arrayToDataTable(jsonData);
var chart = new google.visualization.BarChart(document.getElementById('lastEvaluations'));
chart.draw(data, options);
}
}).responseText;
}
作为arraytodatabasetable函数的一个错误,我得到:
Uncaught Error: First row is not an array.
这可能意味着php脚本获取的数组格式不正确,但是查看他们的文档,我不知道有什么问题。
有人能启发我吗?
1条答案
按热度按时间iswrvxsc1#
$row
很可能是一个物体{}
,而不是数组[]
在你的while
语句,创建一个新数组并将结果中的每个值相加然后将新数组添加到
$myArray
```while($row = $result->fetch_assoc()) {
$rowArray = array();
$rowArray[] = $row["id"];
$rowArray[] = $row["rating"];
$myArray[] = $rowArray;
}
$rowArray = array();
$rowArray[] = "id";
$rowArray[] = "rating";
$myArray[] = $rowArray;
while($row = $result->fetch_assoc()) {
$rowArray = array();
$rowArray[] = $row["id"];
$rowArray[] = $row["rating"];
$myArray[] = $rowArray;
}
var data = google.visualization.arrayToDataTable(jsonData, true);