ahmad chattha
ahmad chattha

Reputation: 19

How I insert data into database with 20 second interval on page refreshing

I have a webpage where I getting live data through API in PHP. when I refresh the page, data updated on-page and also insert into MySQL database from APIs. But I want that I refreshing the page 100 times but data insert into a database with 20-sec interval. For exhale when I refresh the page at 10:00:00 am, the data insert into MySQL at 10:00:00 am. but then again I refresh the page at 10:00:10 am then data not insert into MySQL so on. I want that first data to insert at 10:00:00 am, second data at 10:00:20 am, third data 10:00:40 am and so on.

My Code:

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



        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);

        // Check connection
        if ($conn->connect_error) {
          die("Connection failed: " . $conn->connect_error);
        }
        //echo "Connected successfully";

        $sql =  "INSERT INTO pload(GT1, GT2, ST, TL)
        VALUES($js_result1, $js_result2, $js_result3, $js_result_total)";

        if ($conn-> query($sql) === TRUE)
        {
            //echo "new record created ";
        }else{
            echo "Error"  .$sql . "<br>" . $conn->error;
        }
        $conn->close();

    ?>

Upvotes: 0

Views: 400

Answers (1)

Ad Fortia
Ad Fortia

Reputation: 333

Try reading the actual seconds and execute the insert query only when they are a multiple of 20:

$seconds=date('s');

if(($seconds % 20) == 0) {
   if ($conn-> query($sql) === TRUE)
   {
            //echo "new record created ";
   }else{
            echo "Error"  .$sql . "<br>" . $conn->error;
   }
}

Upvotes: 1

Related Questions