The following code to query a MySQL database and display the results as a Google Chart works just dandy on my local machine. But when I upload the exact same code (logins changed to protect the innocent) to a remote server, the div that is supposed to contain the chart is empty. The page shows only the raw JSON table on which it is based.
<?php
$DB_NAME = '**********';
$DB_HOST = '127.0.0.1';
$DB_USER = '**********';
$DB_PASS = '**********';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "select * from score";
if ($result = $mysqli->query($query)) {
{
$rows = array();
$table = array();
$table['cols'] = array(
array('label' => 'Proj_month', 'type' => 'string'),
array('label' => 'Licenses', 'type' => 'number')
);
while ($row = $result->fetch_assoc()) {
$temp = array();
$temp[] = array('v' => (string) $row['Proj_month']);
$temp[] = array('v' => (int) $row['Licenses']);
$rows[] = array('c' => $temp);
}
}
}
$table['rows'] = $rows;
$jsonTable = json_encode($table);
?>
<html>
<head>
<!--Load the Ajax API-->
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
function drawChart() {
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(<?=$jsonTable?>);
var options = {
title: 'My Licenses',
is3D: 'true',
width: 800,
height: 600
};
// Instantiate and draw our chart, passing in some options.
// Do not forget to check your div ID
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, {curveType: "function"});
}
</script>
</head>
<body>
<!--this is the div that will hold the pie chart-->
<div id="chart_div"></div>
<?php echo $jsonTable; ?>
</body>
</html>