-1

I have a table with some fields.status and starting_timestamps are two of them. the default value of status is T. I want to change this to F after the 10 days. starting_timestamps stores the time of starting.How can i do this? Iam in php-mysql platform

segarci
  • 739
  • 1
  • 11
  • 19
fighter
  • 149
  • 1
  • 8

2 Answers2

1
  1. Create a php file e.g. cron.php with the following code.

    In the following code, variables $host, $mysql_user, $mysql_password, $db, $table are to be replaced with actual database related values.

    <?php
    $host = 'localhost';
    $mysql_user = 'root';
    $mysql_password = 'your password';
    $db = 'databaename';
    $table='actual table name';
    
    $link = mysql_connect($host, $mysql_user, $mysql_password);
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    
    // make foo the current db
    $db_selected = mysql_select_db($db, $link);
    if (!$db_selected) {
        die ('Can\'t use '.$db.': ' . mysql_error());
    }
    
    $result = mysql_query('UPDATE `'.$table.'`
        SET status=\'F\' 
        WHERE
        (UNIX_TIMESTAMP( now( ) ) - `starting_timestamps`) /86400 = 10');
    
    
    if (!$result) {
        die('Invalid query: ' . mysql_error());
    }
    
    //close connection
    mysql_close($link);
    ?>
    

Now using your hosting cpanel, go to cronjobs settings.

Here create a new cronjob which should run everyday. For cron job, enter following command.

/usr/bin/php -f 'path_to_php_script.php'

You can check attached image for how to create cron job.

Here make sure that 'path_to_php_script.php' is real physical path to cron.php file on hosting server.

enter image description here

Alpesh Panchal
  • 1,723
  • 12
  • 9
0

Well Simple.

Create a php file and then from php file run the above query and start a cron job that will run this file everyday at certain time automatically and thus you will get your desire result.

jewelhuq
  • 1,210
  • 15
  • 19
  • If you are in windows but do not know how to setup cron job follow http://stackoverflow.com/questions/7195503/setting-up-a-cron-job-in-windows – jewelhuq Jan 05 '16 at 12:07