how to set up daily php functions or mysql queries
I have wondered for a while now how to this. If I want to take weekly backups of all or many of my tables that stores values that changes every day in mysql. I also want to execute daily functions with php that update update values in my db.
I was thinking of making a stock investing function. Where I have fictional data as the value for various stocks, a value that changes randomly every day for every stock.
Maybe something like this for the last 9 days of the stock price curve.
CREATE TABLE `stocks` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(40) NOT NULL default '',
`day_1` varchar(40) NOT NULL default '',
`day_2` varchar(40) NOT NULL default '',
`day_3` varchar(40) NOT NULL default '',
`day_4` varchar(40) NOT NULL default '',
`day_5` varchar(40) NOT NULL default '',
`day_6` varchar(40) NOT NULL defa开发者_如何学JAVAult '',
`day_7` varchar(40) NOT NULL default '',
`day_8` varchar(40) NOT NULL default '',
`day_9` varchar(40) NOT NULL default '',
if I could execute a php function once a day that made an array of the last 9 days of values. Then just change the day_1
value and use array_push($array, "new_stock_price");
then update the db with the new last_9_days values.
If you're on a Linux / UNIX server, then you can set up a "cron job", which can run any command you like at a specified interval. This command could be a PHP script executed using the command line PHP interpreter).
http://en.wikipedia.org/wiki/Cron
On a *nix system, use a CRON job. On Windows, it's Scheduled Task... Either will execute a script at a given time on any (or a specific) day.
I'd recommend a better table design:
DROP TABLE IF EXISTS `example`.`stocks`;
CREATE TABLE `example`.`stocks` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(45) NOT NULL default '',
`stock_value` varchar(40) NOT NULL default '',
`created_date` datetime NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
This way, if you want to see the last 9 values for a given stock name, use:
SELECT s.name,
s.value,
s.created_date
FROM STOCKS s
WHERE s.name = ?
ORDER BY s.created_date DESC
LIMIT 9
What options are there for executing a PHP script at a certain time every day?
How to implement cron in code (php)?
精彩评论