0

I am retrieving contents from my database(MYSQL) in PHP. The following is my PHP script:

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

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

$myquery = "
SELECT  * FROM  data1
";
    $query = mysql_query($myquery);
if ( ! $query ) {
    echo mysql_error();
    die;
}

$data = array();

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

echo json_encode($data);

mysql_close($server);
?>

The output is a perfectly formatted JSON Object:

[{"ID":"1","Country":"India","Value1":"100","Value2":"200"},{"ID":"2","Country":"India","Value1":"230","Value2":"800"},{"ID":"3","Country":"USA","Value1":"30","Value2":"300"},{"ID":"4","Country":"Sri Lanka","Value1":"320","Value2":"330"},{"ID":"5","Country":"Sri Lanka","Value1":"120","Value2":"90"},{"ID":"6","Country":"Sri Lanka","Value1":"420","Value2":"890"},{"ID":"7","Country":"China","Value1":"20","Value2":"890"},{"ID":"8","Country":"China","Value1":"430","Value2":"999"},{"ID":"9","Country":"Canada","Value1":"200","Value2":"319"},{"ID":"10","Country":"Canada","Value1":"1000","Value2":"29"}]

I want to use this JSON object as input to my D3.js graph.

When I try to create my D3 chart I get the error in my browsers log:

SyntaxError: Unexpected token < in JSON at position 0(…)

The following is my D3 code where i invoke the PHP file and try to parse the JSON:

d3.json("1.php", function(error, data) {
console.log(error);
//parsing operations

});

Any pointers would be appreciated. Thanks

RJP
  • 385
  • 5
  • 19
  • If you load `1.php` in your browser directly, view source, you still see just the clean json data and nothing else? – jszobody Jun 13 '16 at 22:14
  • When i open 1.php in my browser, It downloads the PHP file for some strange reason. I am using a Python SimpleHTTPServer. – RJP Jun 13 '16 at 22:18
  • you need a server (like Apache) that has PHP runtime – Fabricator Jun 13 '16 at 22:19
  • That sounds contrary to *"The output is a perfectly formatted JSON Object"* Where are you seeing this then? – Jeff Puckett Jun 13 '16 at 22:20
  • Jeff, this is when i execute my PHP script directly from terminal. – RJP Jun 13 '16 at 22:21
  • 1
    @RJP ya, but, d3 doesn't execute php from the terminal. =) it loads it over HTTP. so if you can't load it in a browser, d3 certainly can't retrieve it. – jszobody Jun 13 '16 at 22:22
  • install a web server, it's both easy and fun! if you don't want to install a web server, then [this might be helpful](http://stackoverflow.com/q/4301975/4233593), but you might need to change your links to something like this `d3.json("http://localhost:8000/1.php"` and that will open up [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) issues. – Jeff Puckett Jun 13 '16 at 22:25
  • I am now using PHP's built in Web server and that seems to have solved the problem. – RJP Jun 13 '16 at 22:28
  • **WARNING**: If you're just learning PHP, please, do not use the [`mysql_query`](http://php.net/manual/en/function.mysql-query.php) interface. It’s so awful and dangerous that it was removed in PHP 7. A replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and a guide like [PHP The Right Way](http://www.phptherightway.com/) explains best practices. – tadman Jun 13 '16 at 22:46
  • If you want to see what `d3` gets, you can use `d3.text(url, function(t){ console.log(t)} )` to log it in the console, before trying to parse the json out of it. – tarulen Jun 14 '16 at 08:44

0 Answers0