2014-02-25 53 views
0

我試圖顯示總數沒有。行數每5秒異步一次。我創建了一個頁面來獲取行數。但是,想要更新計數而不重新加載頁面。如何異步更新Mysql的行數?

的index.php

<?php 
$con = mysqli_connect("localhost", "root", "pswd", "db_pswd"); 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 
$sql="SELECT * FROM tweets; 
if ($result=mysqli_query($con,$sql)) 
{ 
$rowcount=mysqli_num_rows($result); 
echo $rowcount; 
mysqli_free_result($result); 
} 

不知道如何進行下一步。需要一些幫助來實現這一點!

+0

AJAX和定時器會幫助你.... – user1844933

+0

有n o需要獲取所有表格內容以便對其中的行進行計數。使用'SELECT COUNT(*)FROM tweets',它將返回表中包含* count *行的結果中的一行。 – poncha

+0

@ user1844933:有些代碼會更有幫助 – user3289108

回答

0

在HTML頁面:

Count: 

<div id="rowcount"></div> 

setInterval(function(){ 
    $.ajax({ 
    url:'yourPHP, 
    success:function(data){ 
    $('#rowcount').html(data); 
}, 5000); 

在PHP中:

$sql="SELECT count(id) FROM tweets"; 

這將返回你所需要的數量,打印

+0

嗨,我需要創建另一個HTML或PHP頁面輸入代碼? – user3289108

+0

不,你的顯示html頁面中的DIV和JS代碼,你的PHP文件中的$ sql 其中你的PHP是你的PHP文件 –

+0

如果你不介意,你可以給我完整的html頁面代碼嗎?我完全不熟悉這一點。 PL! – user3289108

0

使用AJAX

setTimeout(function(){$.ajax({ 
url:'GiveURL', /* such as index.php*/ 
method:'get', 
dataType:'text or html', 
success:function(data){ 
    $('#GiveId').html(data); /* give id of your div or textbox*/ 
} 
}); 
},5*1000); 
+0

我是否需要創建任何php頁面或直接在'index.php'中插入? – user3289108

+0

不,你給php文件這樣的名稱index.php或mysqlRow.php –

+0

我的意思是我想要在'index.php'本身插入上面的代碼? – user3289108