I'm trying to use php to connect postgres and visualize the data with d3.js. I encoded the data to json successfully, but when I want to load the data by using d3.js, there's a SyntaxError: Unexpected token < in JSON at position 0(…) index.html:12 undefined.
Here is my code for php:
$db_connection = pg_connect("host=localhost dbname=xxx user=xxx password=xxx");
$result = pg_query($db_connection, "SELECT * FROM taxi_stats.satisfy");
$data = array();
while ($row = pg_fetch_array($result))
{
$data[] = $row;
}
echo json_encode($data);
pg_close($db_connection);
For d3.js
<script>
d3.json("data.php", function(error, data) {
if(error){
console.log(error);
}
console.log(data);
});
</script>