Reputation: 448
Currently I'm struggling to get data loaded from my database to Google Charts, I'm following this tutorial.
I'm trying to load 4 different charts in my webpage. However everytime PHP starts to execute a SQL statement I get this error:
Uncaught SyntaxError: Unexpected token <
Inspect element shows this:
I expect it is the same problem with every chart, so I will only post the code of the first chart, that looks like this:
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Date', 'Visits'],
<?php
$query = "SELECT count(ip) AS count, vdate FROM visitors GROUP BY vdate ORDER BY vdate";
$exec = mysqli_query($con,$query);
while($row = mysqli_fetch_array($exec)){
echo "['".$row['vdate']."',".$row['count']."],";
}
?>
]);
var options = {
title: 'Date wise visits'
};
var chart = new google.visualization.ColumnChart(document.getElementById("columnchart"));
chart.draw(data, options);
}
</script>
The error will start at the same line as PHP starts, so I expect the problem is related to that. I already tries a few things with CDATA, but that dind't work out.
Changing <script type="text/javascript">
to <script type="text/html">
makes the errors disappear, but won't let the script working, so what am I doing wrong here?
Upvotes: 0
Views: 1913
Reputation: 74216
how are you using this, as
http://localhost
orfile:///
? – Fred -ii-
@Fred-ii- thanks! The example you posted worked! Many thanks! – SmashingJummy
So the answer to this is that you need to run as localhost
, not as file:///
directly in your browser, since file:///
will not parse PHP directives.
Upvotes: 3