How to pull data from mysql database and visualize with D3.JS?

后端 未结 6 538
隐瞒了意图╮
隐瞒了意图╮ 2021-02-02 03:16

I have a database in MySQL which I want to visualize in D3.JS. In order to do that, first I want to parse the data in JSON fo

6条回答
  •  梦谈多话
    2021-02-02 03:49

    I have faced problem myself with json_encode, in the end I wrote my own code instead of json_encode. Just you need to set the sql and yoru connection info. Hope you find it useful.

    $conn = new mysqli(DB_Host, DB_User, DB_Password, DB_Name) or die("Connection failed: " . $conn->connect_error);
    $sql = "select  AID as id, deDate_Value as date, nterTime AS counter 
            from xxxx";
    
    //Get the Date and store it in array
    $records = array();
    if ( $result=mysqli_query($conn,$sql) )
        while ( $obj=mysqli_fetch_object($result) )
            $records[] = $obj;
    
    //echo in json format on screen       
    echo "[";
    $comma_1 = "";
    foreach($records as $obj)
    {
        echo $comma_1."{";
        $comma_2 = "";
        foreach($obj as $key => $value)
        {
            echo $comma_2.'"'.$key."\": \"". $value . "\"";
            $comma_2 = ", ";
        }
        echo "}";
        $comma_1 = ", \n";
    }
    

提交回复
热议问题