Update mysql table every twenty four hours automatically - php

I was wondering how I could set up a script (I'm assuming it would be a cron job) that would reset a field in a mysql table every twenty four hours back to zero. I would want it to reset the field for every user not just a specific person. I know nothing about cron jobs unfortunately, but maybe I don't even need to use them. I am very unsure on how to solve this issue. Thanks for the help!

What about using the MySql event scheduler itself?
http://dev.mysql.com/doc/refman/5.6/en/events-overview.html

create a python script that connects to the mysql table and performs the update query.
Create a cron job that runs 1 time every day that executes:
python updateValue.py
It is quite easy, and the best way I can think of doing it.
The crontab argument would look like:
0 0 * * * python updateValue.py

Related

Update database table cyclically every 5 minutes

I have to populate and update one of my MySql database table using a complex and expensive query, based on selection from other table's data. This table doesn't need to be always fully updated when i make a query on it, but i'd like to have a cyclic update every 5 minutes.
This automatic update should be infinite and i need to be sure that it never stops.
After some research, i've found some solution, but i don't know which is better for security and performance.
One of these could be my goal:
Don't create table and make complex query from php every time to get the desired result
Create a php script that repeats cyclically and update table db, maybe using Cron Job.
Update table using a sql event
I think that first solution could be to expensive since query is complex and there could be many request every second, but the result is always updated. I don't have experience about Cron Job, so i can't know if it could be a good idea or not. For the third solution, i still don't have database privileges to run events, but i'd like to know if it could be a valid solution.
All other solutions are welcome, thanks.
Do not use cron. Think about what will happen if one instance goes beyond 5 minutes and the next starts up. Eventually you will have hundreds of copies bogged down stumbling over each other.
Instead have a single job in a loop doing the update. (OK, you could have a cron job to perform a "keep-alive" task of restarting the query if it dies.)
The job would
CREATE TABLE new ...
INSERT INTO new SELECT complex-stuff...
RENAME TABLE real TO old, new TO real;
DROP TABLE old;
loop.
I would opt for Cron Job.
It doesn't clog any request, since it's executed from the operating system.
You can define which user executes the script (cron -u apache -e).
Easy to define interval. (i.e. every 5 minutes */5 * * * * php /path/to/script.php).
It's loggable.
Additional Notes
I had a cron job running under root and it worked just fine. My problem was that the project had a private logging mechanism that each log file would be created by apache user. By running it from root, sometimes the file would be created by root and after that, the scripts being executed by apache would not be able to APPEND to the log.
I also had an emailing script that would run once every 2 minutes that got stuck for 1h. Turns out, because of a bug in the application, an invalid email address (somethingwithoutatsign.com) was inserted into the database, which made the PHPMailer library throws errors. After that, I added a catch block that would send an email to me whenever an exception was thrown. Now, if the script stops running because of bad execution, I get to know right away.

Automatized action on scheduled date in MySQL

I need an action to be realized automatically when a specific date is reached in a MySQL database, so not necessarly when a user is logged in the site and does an action.
This would go for many different entries in the table.
For example, when UTC_TIMESTAMP() reaches 'release_date' for different items, a PHP script would be executed, respectively for each items.
This is something I have never approached before.
I'm reading about Cron Job and MySQL EVENTS. Would that be the way to go?
Thanks in advance! Any help is greatly appreciated.
Lois
The best way to do this is to have a cron job that runs every time interval, you have full control of how often the script will run,
Within this script, you'll basically check the current time and see if it hits your 'release_date', then you run some queries against your db, if not, the script just exits.
so you can create a cron entry that runs every hour, like this
00 * * * * /usr/local/bin/php /path/to/script.php
and inside script.php
#!/usr/bin/php
$release_date = 1383135371;
$now = time();
if($now >= $release_date){
//Connect to database and do your code here
}
This way you'll have much more control of how things will work, this is not even scratching the surface of the powers of cron jobs !, let us know if things aren't clear :)
What you can do is create a PHP script that would run your query, do your comparisons, and then run the appropriate scripts. You would then run this script from the command line via cron. Here's a page that describes it in more detail
http://www.thegeekstuff.com/2011/07/php-cron-job/

Update MySQL every 2 hours with API data

What is the best way to update my database with data every say 2 hours. I am going to be calling an API that returns JSON, and then I want to update that database. It is bringing back snow conditions.
Thanks,
Ryan
It depends what kind of server you are on, but if it is linux based, you can use cron to run your php script every 2 hours.
The cron entry would be something like:
0 */2 * * * /path/to/script
And the script could be a shell script with the form (just an example for my server...):
#!/usr/local/bin/php
<?php
?>
You can create events in MySQL: http://dev.mysql.com/doc/refman/5.1/en/create-event.html
CREATE EVENT e_hourly
ON SCHEDULE EVERY 2 HOUR
COMMENT 'a comment'
DO BEGIN
//Hourly action
END
Running non-SQL command from within the DB-server is not recommended though due to security issues.
(this is only possible through a custom UDF like: http://bernardodamele.blogspot.com/2009/01/command-execution-with-mysql-udf.html#!/2009/01/command-execution-with-mysql-udf.html )
On windows the at command or scheduled tasks can run every 2 hours:
at: http://support.microsoft.com/kb/313565
scheduled tasks: http://windows.microsoft.com/en-US/windows7/schedule-a-task
On Linux crontab is your friend: http://kevin.vanzonneveld.net/techblog/article/schedule_tasks_on_linux_using_crontab/
Use cron and write some script that gets data from api and insert it to database.

How can I get MySQL to run queries on an interval?

I'm creating a web application where every row of a table needs to be processed. I'm spawning one child PHP process per table row. I'm implementing a safety mechanism, so if a PHP process is interrupted processing a row, a new PHP process will spawned to process said row. To do this I'm going to create a new table where all PHP processes check in every 10 seconds or so. I need MySQL to delete all rows that haven't been checked into for 5 minutes or more, so my application will know to create a new PHP child to process that row.
I know it's possible to get MySQL to run queries on an interval, but I don't know how.
~Enter stackoverflow~
Edit: I was hoping to learn how to do this 100% MySQL. Is there no way to set MySQL to run a query every hour, or at a specific time each day or such?
Crontab. You can run the query directly using the mysql client (mysql -uusername -ppassword dbname -e 'query here') or schedule a PHP script which runs the query.
DELETE FROM table WHERE checked_into < CURRENT_TIMESTAMP - INTERVAL 5 MINUTE
MySQL Events are tasks that run according to a schedule. Therefore, we sometimes refer to them as scheduled events. ... Conceptually, this is similar to the idea of the Unix crontab (also known as a “cron job”) or the Windows Task Scheduler.
http://dev.mysql.com/doc/refman/5.1/en/events-overview.html
And here is the lovely syntax: http://dev.mysql.com/doc/refman/5.1/en/create-event.html
One way to run MySQL queries on a certain interval would be to set up a cron job. Assuming you've got full access to your webserver, this should be doable. You'd just make a PHP page that does the SQL operations you want to occur every X time interval, and then set the script to run on that interval via cron jobs. More specifics: http://en.wikipedia.org/wiki/Cron
I think what you are looking for is an event scheduler, first introduced in MySQL 5.1.
On a side note, maybe you should redesign your program a little to avoid the extra layer of event scheduler:
Instead of deleting a row, where a process has not checked in for a while, just have a column with a check in timestamp. Then if some row has a very old check in timestamp, you can spawn a new PHP process for it.

Need to reset database table every week

So I have a game which has a scoreboard based on weekly scores. At the end of the week I would like it to automatically delete all the entries and cache new values again. Issue is I don't know how to trigger such an even via PHP! Any help?
Sounds like you should set up a cron job.
It seems you have unusual database design.
I know no site (gaming or not) that erases weekly data.
The purpose of database is to hold data. Why not to let it just do it's job?
Create a small php file which connects to mysql and TRUNCATES the table ( better and faster to truncate instead of delete all ).
And add this to your crontab.
0 0 * * 0 PATH_TO_PHP myFile.php
Here is a crontab tutorial :
http://clickmojo.com/code/cron-tutorial.html
Generally these type of activities are carried out using Cron Jobs or Task Schedulers which will run periodically to perform the action you would specify.
I think the easiest way would be creating a PHP script which deletes everything on the table, then creating a crontab job for it.

Categories