Chartjs-MYSQL Query and Chart.js and PHP

0👍

Your data is set to json_encode, this is not how chart.js can read it.

Change your code from:

<?php=json_encode($count);?>

To:

["<?php echo implode('","',$count);?>"]

And for the years, from:

<?php=json_encode($years);?>

to:

["<?php echo implode('","',$years);?>"]

The quotes are there to contain anything the isn’t a numerical value, if your positive that all data is numerical, you can remove them, but since it doesn’t hurt to have them, I’ve included them.

0👍

Figured this out:

1: Used this query:

SELECT YEAR(purchased) as years, COUNT(ID) as count FROM computers GROUP BY years

2: Had a TYPO! while ($row = mysql_fetch_assoc($result)) should had been:

while ($row = mysqli_fetch_assoc($result)). I forgot the i in mysql_fetch!

Along with the great help from Greg it is working great now.

Thanks for the help!

Leave a comment