duanmao2774 2016-02-17 00:36
浏览 85
已采纳

如何链接2个PHP脚本在d3中同时运行

I have two php scripts both connected to the same database but each script queries a different table. My question is how do I link these scripts so they run simultaneously. Currently they are running at the same time but completely independent of each other. For example I am working with a scatter plot in d3. One script queries information for the y axis and the other script for the x axis. This is the query in the php script for the x axis:

$myquery = "SELECT  `ID`, `TITLE`, `YEAR`, `In_library` FROM  `papers` where In_library = 1";

YEAR is then called in d3 and used to scale the x axis. The query for the y axis script is:

$myquery = "SELECT `ID_to`, count(*) as `counter` from `citations` group by `ID_to`";

counter is called by d3 and used as the y axis. These are the connections made in d3:

d3.json("connection1.php", function(error, data){
if (error) throw error;
data.forEach(function(d) {
    d['TITLE'] = d.TITLE.toString();
    d['YEAR'] = +d['YEAR']; 
     console.log(d['YEAR']);
    })
d3.json("connection2.php", function(error, data){
//if (error) throw error;
data.forEach(function(d) {
    d['counter'] = +d['counter'];
    console.log(d);
    })  

when ran exactly as above it will just return the points correctly in regards to counter but on top of the y axis with no x values(ie x=0). But when I separate the 2 functions as in close the parentheses from the first line of each function and define all other code ie. svg, domain range etc in BOTH functions they will both execute but mutually exclusive ie. the points displayed are duplicated - one set only with y values and the other set only with x values.

My mission is to integrate these so only one set of the specified points are displayed on the graph using both the x and y axis properly.
I would be grateful for any feedback and happy to post more of the code if I havent posted enough. thanks

EDIT: Here is my php script:

<?php
$username = "root"; 
$password = "";   
$host = "localhost";
$database="samplelit";

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

   $myquery = "SELECT  `ID`, `TITLE`, `YEAR`,`In_library` FROM  `papers` where In_library = 1";
$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);


?>

And the other script is the same with just the other query. Where do I assign the key value pairs? Thanks for the help I am very new to php and appreciate this a lot!

  • 写回答

2条回答 默认 最新

  • douqie1852 2016-02-17 18:06
    关注

    Disclaimer, I'm not a php developer and the below code is untested...

    This should be as simple as:

    <?php
    
    $username = "root"; 
    $password = "";   
    $host = "localhost";
    $database="samplelit";
    
    $server = mysql_connect($host, $username, $password);
    $connection = mysql_select_db($database, $server);
    
    // this is your return
    $data = array(
        query1Results => array(),
        query2Results => array(),
    );
    
    $query1 = mysql_query("SELECT  `ID`, `TITLE`, `YEAR`,`In_library` FROM  `papers` where In_library = 1");
    
    if ( ! $query1 ) {
        echo mysql_error();
        die;
    }
    
    for ($x = 0; $x < mysql_num_rows($query1); $x++) {
        $data['query1Results'][] = mysql_fetch_assoc($query1);
    }
    
    $query2 = mysql_query("SQL FOR SECOND QUERY");
    
    if ( ! $query2 ) {
        echo mysql_error();
        die;
    }
    
    for ($x = 0; $x < mysql_num_rows($query2); $x++) {
        $data['query2Results'][] = mysql_fetch_assoc($query2);
    }
    
    echo json_encode($data);     
    
    mysql_close($server);
    
    ?>
    

    Now back on the JavaScript side:

    d3.json("connection.php", function(error, data){
        if (error) throw error;
    
        // do stuff with results
        console.log(data.query1Results);
        console.log(data.query2Results);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 hc130怎么读写内部flash存储信息
  • ¥15 Axure rp9注册与登录交互
  • ¥15 我下载图形界面重启完就变成这样了,打字也打不了,动也动不了,该怎么解决(操作系统-centos)
  • ¥15 VBA中在窗体中遍历所有checkbox控件,提取出被选中的checkbox的caption值
  • ¥15 在Ubuntu上有什么命令,或者是系统文件能告诉我链接nvme ssd的pcie槽位是不是支持热插拔功能?
  • ¥15 ansys license许可证问题
  • ¥20 QQ号和密码都能正常登录微信 QQ号和密码登录微信显示密码错误
  • ¥15 qiankun主应用注册子应用提示跨域
  • ¥15 单片机RTOS Kernel与应用分离开发,Kernel如何调起应用?
  • ¥15 快手小店商家版APP怎么第三方APP跳转到指定用户聊天界面