-3

Hello I want to update MySQL table automatic at set time like i want to update table at 5:30 i have some values to update in my database at set time automatic. in PHP

$date = date('H:i:s');
if($date == "11:26:00") // 17:30:00 is equal to 5:30 but in 24 system 
{
    $sql1 = "SELECT * FROM No where id='1'";
    $result1 = mysqli_query($mysqli,$sql1) or die(mysqli_error());
    while($row= mysqli_fetch_assoc($result1))
    {
    $Foo= $row['foo'];
    }
    $sql2 = "UPDATE No SET id='2',foo='$foo' where id='2'";
    $result2 = mysqli_query($mysqli,$sql2) or die(mysqli_error());

}
hanks
  • 1
  • 3

1 Answers1

0

Just lie what Ashish said you can use CRON , and if you want you maybe can use this , i did not use before , try it . This code will works once ,so you have to find away to make loop cause the time will change every sec .

Note : Check the second code .

<?php 
    // Database connect

    $date = date('H:i:s'); // Set the time to hours and minutes and seconds format  
    // you can echo $date to check how it looks but it will be something like this : 07:21:48

    if($date == "17:30:00") // 17:30:00 is equal to 5:30 but in 24 system 
    {
        // You can type your code here . 

    }

 ?>

Second Code :

<?php

 // Remember to connect to your database    

$date1 = date_create("00:00");
$date2 = date_create("23:59");
while($date1<=$date2)
{
    date_add($date1,date_interval_create_from_date_string("1 sec"));
    $a = date_format($date1,"H-i-s");
    if($a =='17:30:00')
    {
            $sql1 = "SELECT * FROM No where id='1'";
            $result1 = mysqli_query($mysqli,$sql1) or die(mysqli_error());
            $row= mysqli_fetch_assoc($result1)
                {
                    $Foo= $row['foo'];
                    $sql2 = "UPDATE No SET id='2',foo='$foo' where id='2'";
                    $result2 = mysqli_query($mysqli,$sql2) or die(mysqli_error());
                }
            die;
       }
    else
      {
         // you can type anything here , you can end the process by typing die; or anything you want 
      }

}



?>

You can check this and see how you can loop throw date in php :

I have 2 dates in PHP, how can I run a foreach loop to go through all of those days?

This can help you understand the time and date system and how to change the format for the date in php : How to get the current date and time in PHP?

Community
  • 1
  • 1
Laith
  • 428
  • 4
  • 10