简体   繁体   English

D3 JavaScript从mysql提取数据Uncaught typeError

[英]D3 javascript pulling data from mysql Uncaught typeError

Im currently getting my data from a mysql database using this php : 我目前正在使用此php从mysql数据库获取数据:

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

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

    $myquery = "
SELECT  `this`, `that` FROM  `table`
";
    $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);
    }

    $fp = fopen('empdata.json', 'w');
    fwrite($fp, json_encode($data));
    fclose($fp);    

    mysql_close($server);
?>

I then run this html with the previous json file that I generated 然后,我使用生成的先前的json文件运行此html

d3.json("mypreviouslygeneratedjson.json", function (error, data) {

    data.forEach(function (d) 
    {
        ...
    }
...

It all goes well, I can generate bar charts, pie charts etc. But as soon as I replace the 3 fopen,fwrite and fclose lines with this line: 一切顺利,我可以生成条形图,饼图等。但是,一旦我用这条线替换了3条fopen,fwrite和fclose行,就可以了:

echo json_encode($data);

(plus I replace "mypreviouslygeneratedjson.json" with the php file that I mentioned before) (另外,我用前面提到的php文件替换了“ mypreviouslygeneratedjson.json”)

As soon as I try to do this, I get an error in the console: 尝试执行此操作后,控制台中将出现错误:

Uncaught TypeError: Cannot read property 'forEach' of undefined 未捕获的TypeError:无法读取未定义的属性'forEach'

What is going on here? 这里发生了什么? Im practically using the same json in both cases. 我实际上在两种情况下都使用相同的json。

The problem was that the json generated by my php file included the following mysql warning in it : 问题是我的php文件生成的json中包含以下mysql警告:

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead 不建议使用:mysql_connect():不建议使用mysql扩展,以后将删除:使用mysqli或PDO代替

This resulted in filling my json file with html, making it unreadable by the d3.json 这导致用html填充我的json文件,从而使d3.json无法读取它

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

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