在 PHP 中更新变量而不刷新

Update a variable without refresh in PHP

我需要自动更新名为 $count 的变量(在下面的代码中)。当我的数据库更新时,$count 会立即更新,因为我需要在 android 应用程序中使用 $count 的值,所以我不能每次都刷新 count.php 文件。

<?php
$con=mysql_connect("","","");
mysql_select_db("u607509006_bd1",$con);

$query = "select * from content where status='a'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
echo $count;
mysql_close($con);
?>

只需在客户端使用 Jquery Ajax Request,如下所示:

$.ajax({
  url: "path/to/code/file.php" // change this to your path to your code
}).done(function(data) {
  // This is executed after the ajax request is complete
  // data should be the updated value. You can also return other data // types e.g. JOSN
  $('#counter').text(data);
});

JS Fiddle 示例:https://jsfiddle.net/anik786/nvkdLhv2/2/