I have setup a database and a small website. On the website I should be able to see all the data from the database and also display new entries. For this i have included the "setInterval();". Every second the setinterval starts the function that should read the database. I do not get any errors and the first time the function actually reads the entire database but then it just writes the same thing every second without any of the new inputs from the DB. How can I make it that everytime my function get called it also everything in the database including all neww entries.
var t= "";
setInterval(checkDb, 1000);
function checkDb(){
<?php
session_start();
$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "RFID";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = 'SELECT `id`, `rfid`, `dateandtime` FROM`scans`';
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$answer= "id: " . $row["id"]. " - RFID: " . $row["rfid"]. " - Date:" . $row["dateandtime"]. "<br>";
?>
t= <?php echo json_encode($answer);?> + t ;
document.getElementById("myP").innerHTML = t;
<?php
}
} else {
echo "<br>" ."\n0 results\n";
}
$conn->close();
session_destroy();?>
t="";
};