Do not use mysql_*
functions. Use mysqli_*
instead. When should I use MySQLi instead of MySQL?
Result page:
<table class="users">
<tr>
<th>NAME</th>
</th>Register Time </th>
</tr>
<?php
$query = mysql_query("SELECT * FROM `register`");
$lastId = 0;
while ($row = mysql_fetch_array($query)) {
$lastId = $row['id'];
echo '<tr>';
echo '<td>'.$row['name'].'</td>';
echo '<td>'.$row['reg_time'].'</td>';
echo '</tr>';
}
echo '</table>';
echo '<script>var lastId = '. $lastId .'</script>'; // remember the last id
?>
Javascript loaded at the same page:
$(document).ready(function() {
var usersTable = $(".users");
setInterval(function() {
$.ajax({
url: 'get_users.php?id='+lastId,
dataType: 'json',
success: function(json) {
if (json.length > 0) {
$.each(json, function(key, user) {
usersTable.append('<tr><td>'+user.name+'</td><td>'+user.reg_time+'</td></tr>');
lastId = user.id;
});
}
}
});
}, 10000);
});
get_users.php:
$lastId = (int)$_GET['id'];
$query = mysql_query('SELECT * FROM `register` WHERE `id` >' . $lastId);
$result = array();
while ($row = mysql_fetch_array($query)) {
$result[] = $row;
}
echo json_encode($result);
The main point is to make ajax call every X seconds and append all users that registered after the last user on the current page.
To read: