2015-09-06 102 views
0

我需要自动更新名为$count(在下面的代码中)的变量。当我的数据库更新,$count立即更新,因为我需要在Android应用程序中使用值$count,所以我无法每次刷新count.php文件。更新变量而不刷新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); 
?> 
+0

你可以使用jQuery或AJAX将数据发送到一个单独的更新页面,但该将不会更新当前页面上的计数。 – Kuya

+0

你不能只使用ajax请求。 ajax请求可以返回一个值,并且你也更新你的页面。我对Android开发不太确定,但可以轻松地为您提供HTML + JS/JQuery中的解决方案 –

回答

1

在客户端只需使用一个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小提琴例:https://jsfiddle.net/anik786/nvkdLhv2/2/