访问MySQL数据库 - D3

时间:2012-12-08 23:54:23

标签: php mysql d3.js

我有一个基本的折线图并尝试访问mySQL数据库。我在PHP的某个地方出错和/或如何处理JSON返回 - 任何人都可以帮忙解决一些问题吗? 目前图表根本没有显示并收到错误消息“Uncaught Syntax Error:Unexpected token>”对于html标签 - 不知道为什么语法在我看来是正确的?

http://bl.ocks.org/5fc4cd5f41a6ddf2df23

“getdata.php”如下:

    <?php

    $username="******"; 
    $password="******";   
    $host="********";
    $link=mysql_connect($host,$username,$password)or die("Unable to connect to MySQL");

    @mysql_select_db($link) or die( "Unable to select database");

    $result = mysql_query("SELECT reading, COUNT(TYPE) AS 'type' FROM    TestSourceSampleData ");
    $rows = array();

    while($r = mysql_fetch_assoc($result)) {
    $rows[$r['reading']] = $r['type'];}

    echo json_encode($rows);
    mysql_close();

    ?>

2 个答案:

答案 0 :(得分:0)

尝试将svg标记放在静态部分中;我认为遗漏了xmlns。您可以将svg:命名空间放在顶部。

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="290">

还可以在浏览器中输入getdata.php进行检查(查看数据是否正确传送)。

答案 1 :(得分:0)

这似乎已经成功了 - 虽然声明的措辞和格式不正确:

getdata.php现在如下:

    <?php
    $username="***"; 
    $password="****";   
    $host="*****";

    $link=mysql_connect($host,$username,$password)or die("Unable to connect to MySQL");

    mysql_select_db("****", $link) or die( "Unable to select database" );

    $result = mysql_query("SELECT reading FROM TestSourceSampleData", $link)
      or die ("Unable to run query");

    while ($row = mysql_fetch_assoc($result))
    {       
    $reading = $row["reading"];
    echo json_encode($row);
    }    
    mysql_close($link);
    ?>