php使用mysql进行长轮询


php long polling using mysql

我发现了一个使用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,content,user_id,posted_time

我怎么知道是否有新帖子?

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

请参阅以下代码,以获取常规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);
?>