![](/img/trans.png)
[英]How to return data with its data from a apivot table as a JSON response?
[英]How to return rows of data from a table with json?
我正在嘗試長時間輪詢,並且它可以與textfile一起使用,但是我無法使其從表中返回所有內容。
var timestamp = null;
function waitForMsg() {
$.ajax({
type: 'GET',
url: 'update.php?timestamp=' + timestamp,
async: true,
cache: false,
success:function(data) {
// alert(data);
var json = eval('('+data+')');
if (json['msg'] != '') {
$('div.alltext').html(json['msg']); // MD
}
timestamp = json['timestamp'];
setTimeout('waitForMsg()', 1000);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
// alert('error '+textStatus+'('+errorThrown+')');
setTimeout('waitForMsg()', 1000);
}
});
}
update.php
$filename = 'test.txt';
$lastmodif = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
$currentmodif = filemtime($filename);
while ($currentmodif <= $lastmodif) {
usleep(10000);
clearstatcache();
$currentmodif = filemtime($filename);
}
// how to rewrite the following?
$response = array();
$response['msg'] = file_get_contents($filename);
$response['timestamp'] = $currentmodif;
echo json_encode($response);
如何重寫最后一部分以從數據庫表中獲取所有內容?
編輯:
我嘗試了以下操作,但它僅返回一個結果,而不是最新的結果,而是之前的結果。
$response = array();
while($row = mysqli_fetch_assoc($r)) {
$nou = $row['f1'];
$nru = $row['f2'];
$ntext = $row['content'];
$ntime = $row['time'];
}
$response['msg'] = $ntext;
$response['timestamp'] = $currentmodif;
您正在循環外分配值,因此您將獲得最后的結果。 在循環內分配值。
$response = array();
while($row = mysqli_fetch_assoc($r)) {
$nou = $row['f1'];
$nru = $row['f2'];
$response['msg'] = $row['content'];
$response['timestamp'] = $row['time'];
}
echo json_encode($response);
使用array
替換$ntext = $row['content'];
與$ntext[] = $row['content'];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.