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条)

报告相同问题?

悬赏问题

  • ¥60 不懂得怎么运行下载来的代码
  • ¥15 CST导出3D模型图为什么和软件显示不一样?
  • ¥15 加热反应炉PLC控制系统设计(相关搜索:梯形图)
  • ¥15 python 用Dorc包报错,我的写法和网上教的是一样的但是它显示无效参数,是什么问题
  • ¥15 经过滑动平均后的一维信号还原用什么结构好呢?
  • ¥15 指定IP电脑的访问设置
  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 为什么devc++编译项目会失败啊
  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒