我在獲取最新的數據時遇到了一些麻煩。我只想找到數據庫中是否有新的數據,並將記錄的數量打印回我的頁面。我有ajax工作,它有最後notification_id它發送到php。我只需要讓PHP選擇任何新數據,然後將其發回並打印出來。Ajax/php獲取最新數據通知
我還沒有嘗試過輪詢,但是當我有這個工作並且瞭解它更好一點時,它會查看它。
<? $call="SELECT * FROM notifications WHERE notification_targetuser='$user1_id' AND notification_status=1 ORDER BY notification_id DESC LIMIT 1";
$chant=mysqli_query($mysqli,$call) or die(mysqli_error($mysqli));
while($notification_id=mysqli_fetch_array($chant))
{
?>
<script type="text/javascript">
setInterval(function(){
var notification_id="<?php echo $notification_id['notification_id'] ;?>"
$.ajax({
type: "GET",
url: "viewajax.php?notification_id="+notification_id,
dataType:"json",
cache: false,
success: function(data){
$(".mess"+notification_id).prepend("<span class'mess' id='mes'>"+response['notification_id']+"</span>");
}
});
},20000);
</script>
<? }?>
echo'';
PHP viewajax.php - 改成了JSON - 但得到{ 「notification_id」:空}在響應
<?php
session_start();
include"database.php";
if(isset($_GET['notification_id']))
{
$id=$_GET['notification_id'];
$user1_id=$_SESSION['id'];
$json = array();
$com=mysqli_query($mysqli,"select notification_id from notifications where notification_id>'$id' AND notification_targetuser='$user1_id' AND notification_status=1");
$resultArr = mysqli_fetch_array($com);
$json['notification_id'] = $resultArr['notification_id'];
mysqli_free_result($com);
echo json_encode($json);
}?>
嘗試在沒有'json_encode'的情況下回顯'$ json'變量來檢查'notification_id'是否返回任何id或者仍然爲null。有時'<?php'可以工作,但'<?'不會 –
我在響應中獲得Array。 – Dave