I have a little project. I want to generate a chart with CanvasJS framework using a MySQL database.
The database is named sti2d and the table is called sin_project It contains this:
mysql> SELECT * FROM sin_project;
+------+-------------------+-------------+
| id | wall | temperature |
+------+-------------------+-------------+
| 2 | Brique Pleine | 19.82 |
| 3 | Brique Creuse | 0.00 |
| 4 | Béton Cellulaire | 0.00 |
| 5 | Intérieur Maison | 0.00 |
| 1 | Parpaing | 0.00 |
+------+-------------------+-------------+
5 rows in set (0.00 sec)
I use a file called data.php to do 2 actions. Read the table sql and return a JSON information of the table data. The file data.php is here:
<?php
header('Content-Type: application/json');
$con = mysqli_connect("localhost","root","mypasswd","sti2d");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to DataBase: " . mysqli_connect_error();
}else
{
$data_points = array();
$result = mysqli_query($con, "SELECT * FROM sin_project");
while($row = mysqli_fetch_array($result))
{
$point = array("label" => $row['wall'] , "y" => $row['temperature']);
array_push($data_points, $point);
}
echo json_encode($data_points, JSON_NUMERIC_CHECK);
}
mysqli_close($con);
?>
And then, i use this page to draw the chart
<!DOCTYPE HTML>
<html>
<head>
<script src="other/js/canvasjs.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.getJSON("http://localhost/data.php", function (result) {
var chart = new CanvasJS.Chart("chartContainer", {
data: [
{
type:"column",
dataPoints: result
}
]
});
chart.render();
});
});
</script>
</head>
<body>
<div id="chartContainer" style="height: 300px; width: 100%;"></div>
</body>
</html>
But, I have no chart and I don't see my error.
Go to ' http://localhost/data.php ' in your web browser. See if anything shows up.
If not, try adding
error_reporting(E_ALL);
To the top of your data.php file.
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.