I have a google bar chart that I am trying to populate with data using PHP accessing a mySQL database but when I try and load my webpage I get the following error:
Uncaught (in promise) SyntaxError: Unexpected token u in JSON at position 0 at Function.parse [as parseJSON]
Here is what my PHP script currently returns:
"['Priority', 'Automated', 'isAutomatable', 'isNotAutomatable'],""['All', 216, 861, 44],""['P1', 213, 568, 34],""['P2', 1, 148, 6],""['P3', 2, 136, 3],""['P4', 0, 7, 1],""['P5', 0, 2, 0],"
This is the chart I am looking to create: Clustered Column Chart
You can find the chart here also: https://developers.google.com/chart/interactive/docs/gallery/columnchart under the " Creating Material column charts " section of the page
I have included my HTML header where I am creating the google chart and the part of my PHP script where I am getting the data from my database.
HTML part
<script type="text/javascript">
google.charts.load('current', {'packages':['bar']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
url: "localhost:8080/getData.php",
dataType: "json", // type of data we're expecting from server
async: false // make true to avoid waiting for the request to be complete
}).responseText;
var data = google.visualization.arrayToDataTable($.parseJSON(jsonData));
var options = {
chart: {
title: 'Automation of Tests Progression'
}
};
var chart = new google.charts.Bar(document.getElementById('columnchart_material'));
chart.draw(data, google.charts.Bar.convertOptions(options));
}
</script>
PHP script
header('Access-Control_Allow-Origin: *');
header('Content-Type: application/json');
// Create connection and select db
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
// Get data from database
$result = $db->query("select platform,Priority,Automated,isAutomatable,isNotAutomatable,Total from automation_progress where platform = 'Cox' order by priority");
#echo ['Priority', 'Automated', 'isAutomatable', 'isNotAutomatable'],
if($result->num_rows > 0){
echo json_encode("['Priority', 'Automated', 'isAutomatable', 'isNotAutomatable'],");
while($row = $result->fetch_assoc()){
echo json_encode ("['".$row['Priority']."', ".$row['Automated'].", ".$row['isAutomatable'].", ".$row['isNotAutomatable']."],");
}
}
UPDATE:
I have changed part of the PHP script to gather the results in an array and encode the array. The data is now is now in correct JSON format but I am still seeing the same error when I try and return the data into the Google Chart. The issue is happening in the HTML during:
var data = google.visualization.arrayToDataTable($.parseJSON(jsonData));
You do not need to so such complicated stuff : just store all your data into an array in php and then encode it to json and echo it
if ($result->num_rows > 0) {
$arrayToEncode = ['Priority', 'Automated', 'isAutomatable', 'isNotAutomatable'];
while ($row = $result->fetch_assoc()) {
$arrayToEncode[] = [$row['Priority'], $row['Automated'], $row['isAutomatable'], $row['isNotAutomatable']];
}
echo json_encode($arrayToEncode);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.