我正在使用PHP从我的数据库(MYSQL)中检索内容。以下是我的PHP脚本:
<?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);
?>
输出是完美格式化的JSON对象:
[{"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"}]
我想使用这个JSON对象作为我的D3.js图的输入。
当我尝试创建我的D3图表时,我在浏览器日志中收到错误:
SyntaxError: Unexpected token < in JSON at position 0(…)
以下是我的D3代码,我调用PHP文件并尝试解析JSON:
d3.json("1.php", function(error, data) {
console.log(error);
//parsing operations
});
任何指针都将不胜感激。感谢