【问题标题】:php long polling using mysql使用mysql的php长轮询
【发布时间】:2013-12-08 22:48:46
【问题描述】:

我找到了一个使用 php 长轮询的脚本。 它使用以下代码查看文本文件是否更改并返回内容。 这是由浏览器接收的。

如何更改它以读取表格并查看是否有新事件?

$filename= dirname(__FILE__)."/data.txt";

$lastmodif = isset( $_GET['timestamp'])? $_GET['timestamp']: 0 ;
$currentmodif=filemtime($filename);


while ($currentmodif <= $lastmodif) {
usleep(10000);
clearstatcache();
$currentmodif =filemtime($filename);
} 

$response = array();
$response['msg'] =Date("h:i:s")." ".file_get_contents($filename);
$response['timestamp']= $currentmodif;
echo json_encode($response);

我有一张桌子,里面有“帖子”。 post_id,内容,user_id,posted_time

我怎么知道有没有新帖子?

【问题讨论】:

    标签: php jquery mysql ajax long-polling


    【解决方案1】:

    您基本上只需要获取一个新结果,就是这样,如果您想检查自上次执行脚本以来是否有新结果,您必须将输出保存在某处(这是您想要做的吗?)

    查看以下代码,了解常规 MySQL 查询:

    <?php
    // Connect to your MySQL DB
    @$db = mysqli_connect("localhost", "root", "", "database");
    
    // Check connection and echo if an error occurs
    if (mysqli_connect_errno()) {
      printf("Connection failed: %s\n", mysqli_connect_error());
      exit();
    }
    
    // Execute SQL Query, replace this with your Query (maybe the one I put in fits for your needs)
    $query = mysqli_query($db, "SELECT * FROM posts");
    
    // Transform the result into an array if your you got new elements in the MySQL DB.
    $resultat = mysqli_fetch_assoc($befehl);
    
    // Close connection
    mysqli_close($db);
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      • 2012-10-06
      • 2011-10-20
      • 1970-01-01
      相关资源
      最近更新 更多