用php连接postgres并使用d3.js进行可视化

时间:2016-05-13 20:21:13

标签: php json postgresql d3.js

我尝试使用php连接postgres并使用d3.js可视化数据。我成功地将数据编码为json,但是当我想使用d3.js加载数据时,会出现一个SyntaxError:Unexpected token<在JSON的0位(...) index.html:12未定义。

这是我的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);

对于d3.js

<script>
d3.json("data.php", function(error, data) {
    if(error){
        console.log(error);
    }
    console.log(data);
});
</script>

2 个答案:

答案 0 :(得分:0)

将标题内容类型从“text / javascript”更改为“text / html”。

或者可能是令牌'&gt;'之一的错误不包含单引号或双引号。

答案 1 :(得分:0)

最后我明白了。 添加一行php     标题(&#39;内容类型:application / json; charset = utf-8&#39;); 适合我。