to get d3.json to work, i had to call it the following way (my php file generates the json data):



you may use jquery which is more popular as follows:

$.getjson( "ajax/test.json", function( data ) {


afaik there aren't any libraries that allow javascript to connect to a mysql database directly. instead consider exposing your data via a web service (using any number of server side technologies:, php, ruby on rails etc) and serialise your data to json in the response


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";


short answer: web service.

basically, you'd want to make a web service that will return json data (for instance) to connect to d3.json() calls.

i would recommend you to use python to quickly do something extending simplehttpserver, or go with a web service framework such as web2py.


the following is a php script that you should be able to save somewhere as a file (let's say you call it 'getdata.php') accessible from your html file with your d3 code in it. when called it will return data from your mysql database in a json format (so long as the database server isn't outside your domain);

    $username = "******"; 
    $password = "******";   
    $host = "******";

    $server = mysql_connect($host, $user, $password);
    $connection = mysql_select_db($database, $server);

    $myquery = "
    query here

    $query = mysql_query($myquery);

    if ( ! $query ) {
        echo mysql_error();

    $data = array();

    for ($x = 0; $x < mysql_num_rows($query); $x++) {
        $data[] = mysql_fetch_assoc($query);

    echo json_encode($data);     


obviously you would need to enter appropriate details for username, password, host and database. you would also need to include an appropriate query for your data so that it returned what you were looking for. something along the lines of (and this is only a guess);

select `datetimetaken`, `reading` from `tablename`

which would return a list of time stamps and values from a table called tablename with columns called datetimetaken and reading. then when you go to read in your json file you would use the following syntax for the code where you would be reading in your json;

d3.json("getdata.php", function(error, data) {

hopefully that's close to what you're looking for. i've tested it locally and it all seems to work..

i've put together a post to go over local installation of a simple wamp server and setting up a query on the mysql database from d3.js here

this is pretty much the same situation as accessing mysql database in d3 visualization

Related Query

More Query from same tag