如何以及在何处使用长轮询脚本进行php查询

如何以及在何处使用长轮询脚本进行php查询,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,我想使用长轮询来更新像facebook这样的墙贴 下面是我使用的长轮询脚本,如果我在msgsrv.php中使用普通html文本,它会显示更改 现在,我想进行一个php查询,然后添加新数据(如果在 数据库 如果我在msgsrv.php中添加php查询,则显示所有php代码。所以我认为我的查询在脚本或其他任何地方都不正确 如何在长轮询脚本中进行php查询以及在何处进行查询,以在index.php中回显查询结果 index.php <script type="text/javascript" s

我想使用长轮询来更新像facebook这样的墙贴

下面是我使用的长轮询脚本,如果我在
msgsrv.php
中使用普通html文本,它会显示更改

现在,我想进行一个php查询,然后添加新数据(如果在 数据库

如果我在
msgsrv.php
中添加php查询,则显示所有php代码。所以我认为我的查询在脚本或其他任何地方都不正确

如何在长轮询脚本中进行php查询以及在何处进行查询,以在index.php中回显查询结果

index.php

<script type="text/javascript" src="client.js"></script>
</head><body>
<div id="response"></div>
<body>
server.php

<?php
set_time_limit(0);

$data_source_file = 'msgsrv.php';

while (true) {

// if ajax request has send a timestamp, then $last_ajax_call = timestamp, else $last_ajax_call = null
$last_ajax_call = isset($_GET['timestamp']) ? (int)$_GET['timestamp'] : null;

// PHP caches file data, like requesting the size of a file, by default. clearstatcache() clears that cache
clearstatcache();
// get timestamp of when file has been changed the last time
$last_change_in_data_file = filemtime($data_source_file);

// if no timestamp delivered via ajax or msgsrv.php has been changed SINCE last ajax timestamp
if ($last_ajax_call == null || $last_change_in_data_file > $last_ajax_call) {

    // get content of msgsrv.php
    $data = file_get_contents($data_source_file);

    // put msgsrv.php's content and timestamp of last msgsrv.php change into array
    $result = array(
        'data_from_file' => $data,
        'timestamp' => $last_change_in_data_file
    );

    $json = json_encode($result);
    echo $json;

    break;

} else {
    sleep( 1 );
    continue;
}
}
?>

Interval应该用javascript而不是php设置。此外,您似乎没有根据时间戳调整查询。不确定您为什么要在这个场景中使用
file\u get\u contents()
先生,我用
msgsrv.php
替换了
data.txt
文件。我的server.php和clint.js也是从web源代码收集的。我对这个代码也不是很了解。
<?php
set_time_limit(0);

$data_source_file = 'msgsrv.php';

while (true) {

// if ajax request has send a timestamp, then $last_ajax_call = timestamp, else $last_ajax_call = null
$last_ajax_call = isset($_GET['timestamp']) ? (int)$_GET['timestamp'] : null;

// PHP caches file data, like requesting the size of a file, by default. clearstatcache() clears that cache
clearstatcache();
// get timestamp of when file has been changed the last time
$last_change_in_data_file = filemtime($data_source_file);

// if no timestamp delivered via ajax or msgsrv.php has been changed SINCE last ajax timestamp
if ($last_ajax_call == null || $last_change_in_data_file > $last_ajax_call) {

    // get content of msgsrv.php
    $data = file_get_contents($data_source_file);

    // put msgsrv.php's content and timestamp of last msgsrv.php change into array
    $result = array(
        'data_from_file' => $data,
        'timestamp' => $last_change_in_data_file
    );

    $json = json_encode($result);
    echo $json;

    break;

} else {
    sleep( 1 );
    continue;
}
}
?>
include("../db.php");
    global $dbh;
    $results = mysqli_query($dbh,"SELECT * FROM comments_lite WHERE qazi_id='1012' ORDER BY date DESC LIMIT 1") or die(mysqli_error($dbh));

while($rows = mysqli_fetch_assoc($results)) {
    $id = $rows['id'];
    $qazi_id = $rows['qazi_id'];
    $detail = $rows['detail'];
    $username = $rows['username'];
    //etc. all

    echo '<div id="nm">'.$username.'</div><div id="dt">'.$detail.'</div>'