AJAX mysql php select every 5 seconds example

index.html

<!DOCTYPE html>
<html>
<body>

<div align="center" id="timeval">--:--:--</div>
<script src="jquery.js"></script>
<script>
$(document).ready(function()
{
   //ajaxTime.php is called every second to get time from server
   var refreshId = setInterval(function()
   {
     $('#timeval').load('select.php?randval='+ Math.random());
   }, 1000);

   //stop the clock when this button is clicked
   $("#stop").click(function()
   {
     clearInterval(refreshId);
   });
});
</script>

</body>
</html>

———————————————————————
select.php

<?php
$servername = "localhost";
$username = "root";
$password = "123";
$dbname = "mydb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}.
?>
<table>

<?php
$sql = "SELECT * FROM my_table ORDER BY uid DESC limit 25";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo '<tr>';
        echo '<td>' . $row["row1"] . '</td><td>' . $row["row2"] .'</td><td>' . $row["row3"] . '</td>';
        echo '</tr>';
    }
} else {
    echo "0 results";
}
$conn->close();
?>


</table>

———————————————————————
P.S. put jquery.js in current directory

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>