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
Asked
Active
Viewed 1,780 times
-1
-
1how do you want to do this by php or from mysql. – jewelhuq Jan 05 '16 at 11:30
-
Numerous options - run a cron, remove the status column and just calculate it in queries, update on select – Steve Jan 05 '16 at 11:37
-
@jewelhuq .i want auto updation of the table.whats the way? – fighter Jan 05 '16 at 11:56
2 Answers
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.

Alpesh Panchal
- 1,723
- 12
- 9
-
-
You can write above query into php script and set that script into cronjob to run every day. e.g. /usr/bin/php -f path_to_php_script – Alpesh Panchal Jan 05 '16 at 12:03
-
@AlpeshPanchal You should include this information in your "Answer", otherwise it will be removed as not answering the question. The 10 days is mentioned in the original question. – Cindy Meister Jan 05 '16 at 16:24
-
I added detailed instructions in the above Answer for your reference. – Alpesh Panchal Jan 06 '16 at 16:15
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