0

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="";
        };
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
ThaSam
  • 13
  • 7
  • 1
    please take a look here, it wil explain a lot: http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – Nanne Mar 08 '17 at 13:12
  • 1
    you should use `AJAX` from this purpose – codtex Mar 08 '17 at 13:21

1 Answers1

0

You can just load the php script with jquery load when you first open the page then after x seconds load the script using setInterval

<script type="text/javascript">
        $('document').ready(function(){
            $('#myP').load('php-script.php');//load the script immediately 
            setInterval(function(){setUpdates()}, 10000);

        });

        function setUpdates(){

            $('#myP').load('php-script.php');
        }

    </script>

php-script.php

<?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>";
    }
} else {
    echo "<br>" . "\n0 results\n";
}
$conn->close();
session_destroy(); // I dont think u need this
?>
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
  • Thx, this looks much better. Was indeed trying that way meanwhile. It doesnt seem to work yet. Tried your code and it does not give any errors but neither does it put anything in the paragraph. Tried to give it double quotes and changed the name of the php page. – ThaSam Mar 08 '17 at 13:50
  • You have Jquey loaded? what does your console say and error logs? – Masivuye Cokile Mar 08 '17 at 13:51
  • It works now, just had to echo $answer in the php code: while ($row = $result->fetch_assoc()) { $answer = "id: " . $row["id"] . " - RFID: " . $row["rfid"] . " - Date:" . $row["dateandtime"] . "
    "; echo $answer; }
    – ThaSam Mar 08 '17 at 13:55