简体   繁体   English

d3从mysql数据库导入数据返回“未定义”

[英]d3 importing data from mysql database return “undefined”

I'm making a map and I have a huge dataset (almost 4 million record) so I stored it in a mysql database and I query it only for the data I really need to make the map. 我正在制作地图,并且我有一个庞大的数据集(近400万条记录),因此我将其存储在mysql数据库中,并且仅查询制作地图所需的数据。
I have a pend.php file that return the data in json format and I want to use it in d3 我有一个pend.php文件,它以json格式返回数据,我想在d3中使用它

d3.json("data/pend.php", function(error, data) {
  console.log(data);
})

But I only get "undefined". 但是我只会得到“未定义”。
If I run pend.php directly in the browser I get the json correctly formatted. 如果我直接在浏览器中运行pend.php,我将得到正确格式化的json。 Here the pend.php code: 这里的pend.php代码:

<?php
$con=mysqli_connect('localhost', 'root', 'root', 'commuting');
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con, "SELECT ComResId,ComLavId,NumSum
FROM itaSmall"); 

$number = mysqli_num_rows($result);
$i = 1;
echo "["; 
while($row=mysqli_fetch_array($result))  
    {  
        echo "{'id':'" . $row['ComResId'] . "','des'" . $row['ComLavId'] . "','n':'" . $row['NumSum'] . "'}";  
        if ($i < $number)
           {
               echo ',';
           }
        $i ++;
    }  
echo "]"; 

mysqli_close($con);
?>

And a sample of the outputed json: 以及输出的json的示例:

[{'id':'1001','des':'1001','n':'469'},{'id':'1001','des':'1004','n':'1'},...

Any idea on what is going wrong? 有什么问题的主意吗?
thanks 谢谢
daniele 丹尼尔

I think that the parameter "error" it's invalid in that function, try with: 我认为参数“错误”在该函数中无效,请尝试:

d3.json("data/pend.php", function(data) {
  console.log(data);
})

And if you need to get the error function try that: 如果需要获取错误函数,请尝试以下操作:

d3.json("data/pend.php", function(data) {
   console.log(data);
}).fail(function() {
   console.log( "error" );
})

OR: 要么:

d3.json("data/pend.php", function(data) {
    console.log(data);
}).error(function() {
    console.log( "error" );
})

Solved.... 解决了....
I was using single quote (instead of dubble quote) to wrap string inside the json!!! 我正在使用单引号(而不是dubble引号)将字符串包装在json中!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM