I have done a lot R&D, tried lots of question on stackoverflow and from other site too but unable to understand what i am doing wrong. I will be really thankful if help me to point where i am wrong.
Also tried this too if code is skipping before response but not use. Returning array from d3.json()
D3 code
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
d3.json("chart_data.php", function(error, data){
json_Data = data;
console.log("Error:"+error);
console.log(json_Data);
});
</script>
</body>
</html>
Console for error is
Error:SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Console for data is
undefined
If I run php file direct in browser I am getting:
[{"value":"1","date":"2016-03-29"},{"value":"0","date":"2016-03-30"},{"value":"3","date":"2016-03-31"},{"value":"2","date":"2016-04-01"},{"value":"2","date":"2016-04-02"},{"value":"5","date":"2016-04-03"},{"value":"1","date":"2016-04-04"},{"value":"1","date":"2016-04-05"},{"value":"0","date":"2016-04-06"}]
This is my PHP
<?php
require 'db_connection.php';
$sql = "SELECT `y-axis` as value, `x-axis` as date FROM site_data";
$result = $con->query($sql);
$data = array();
if ($result->num_rows > 0) {
$count = 0;
while($row = $result->fetch_assoc()) {
$data[$count]['value'] = $row['value'];
$data[$count]['date'] = $row['date'];
$count++;
}
}
echo json_encode($data);
$con->close();
?>
DB connection file
<?php
$con = mysqli_connect("localhost","root","","analytic");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
PHP file and D3 file is on same directory.