Update MySQL every 2 hours with API data - php

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.

Related

Posting data from database at regular time intervals automatically in php

I have questions stored in my database. I want to regularly post one question on my website from the database at a 24 hr interval automatically. Is there a way I can do that ?
You can do this with steps:
Create normal PHP-script which will post your questions.
Schedule your script with standard OS scheduler. It is cron for *nix (Win-versions exist too) or AT for Windows. To define certain interval - you should read scheduler's manual (for cron format is provided here)
Example (cron)
0 2 * * * /usr/bin/php /path/to/insert/script.php
-in this case every day at 02:00 AM cron will try to execute command /usr/bin/php /path/to/insert/script.php - i.e. if your script.php will extract your question from DB and post it - that will do the stuff.
Yes you can do it by using Cron job . Set time interval and your file script location. It will automatically hit your script on that time interval.
Here is good tutorial : http://docs.phplist.com/SetupCronJob.html
Providing you could create a PHP script to select a different question each time, all you'd need to do would be to set up a cron to run the PHP script every 24 hours. You can find more info on cron here.
You should look into MySQL date functions.
A contrived example would be using CURDATE():
SELECT * FROM questions WHERE publish_date = CURDATE()
Storing the publish_date will mean you can dynamically load the question when that date arrives.
The only way to do it correctly is to use cron jobs. You should take a look at the administration panel of your hosting service.
Write a script that will post one question on your website everyday and set a cron job to run that script once a day and you are done.
How to set a cron job , ask you hosting service provider , most of the hosts have this feature in cpanel
Yes, you can. I will shortly outline the two most common solutions. The difficulty rises that PHP is not an always running program, but is a language executed on request and then shutdown on completion.
Have some sort of init.php file on your webserver which is being included on every page. That script will check whether the time has passed since last question, and push a new question.
On the other hand, you can add a cronjob which will execute your php script pushing the question. This solution is more robust, but requires access to a webserver you might not have.
Create a php file put the code to fetch question form your database
then set cronjob to excecute the file on perticular time or also
you can execute file by including it your login or any other page which
lods first by including that php file file so that when first user logs in
it will execute.
Steps
Create a PHP script to select and post a particular question randomly.
In your main php script write an AJAX method(which will load the PHP script) which can be called using setInterval() using the following syntax-
setInterval("AJAX_fun()", 24*3600*1000);
This statement will call the AJAX function in a periodic interval of 24hrs. For that you must know AJAX. I mean what should be the body of the AJAX to load the PHP script that you must have an idea of.
Another alternative
You can simply reload the page using javascript setInterval() function
i.e. <script>setInterval("window.location.reload()", 24*3600*1000);</script> and before that you have to select a question from the database randomly using a PHP logic.

Update mysql table every twenty four hours automatically

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

How to delete mysql row after time passes?

I have no idea where to start with this one:
I have a database that stores postID and Date.
What I want to do is have my website auto delete all rows where Date is less than today. This script can't have any user input at all. No button clicks, nothing. The script must run every day at midnight.
I've been looking all over the place for something that does this and I've found absolutely nothing.
You can use PHP script and use cron job on your cpanel.
Example:
cronjobcommand.php
<?php
include 'your_db_connection';
mysql_query("DELETE FROM your_table_name WHERE Date < NOW()");
?>
I have attached a screenshot below for your more reference.
For those out there who are on a shared hosting, like 1and1's, and can't use cron, here are 2 alternatives :
mysql events enable you to place a time trigger on mysql, which will execute when you'll want, without having to be fired by any kind of user input
if you cannot create mysql events because you're on 1and1 :(, an alternative is to use webcron
You just need to tell webcron the url of the php script you'd like to be run, and they'll trigger it for you at the intervals you want
Why using cronjobs everyday?? Why not filter data on output. For example in your select check if post date equals today with adding a simple where:
SELECT * FROM `posts`
WHERE (DATE(`post_date`) = DATE(NOW()));
This way you're not required to do your database managements/cronjobs on any special time and it will be used just for database managements. Afterwards you can delete unnecessary data at any time using by mysql command like:
DELETE FROM `posts` WHERE (
DATE(`post_date`) < DATE(NOW())
)
Most hosts provide a cron(8) service that can execute commands at specific times. You use the crontab(1) program to manage the crontab(5) file the describes when to run which commands.
There's a lot of functionality available to you, but if you write a program (shell script, php script, C program, whatever) that runs the appropriate MySQL commands, you can call the program via cron(8) in an entirely hands-off fashion.
Run crontab -e to edit your current crontab(5) file. If none exists, hopefully you'll get one with a helpful header. If not, copy this:
# m h dom mon dow command
The columns indicate the minute, hour, day of month, month, and day of week to execute commands. All the numbers in the columns are essentially ANDed together to decide when to run commands.
Thus, midnight every night would look like this:
0 0 * * * /path/to/executable
It's remarkably flexible, so put some time into the documentation, and you'll find many uses for it.
You should set cron job (scheduled tack.) for it.
A cron job is an automated program setup for Linux and Unix operating systems. It allows the user to execute several commands or functions at a specific time and date.
you have cron Job in your cpanel setup. first you need to make a php script with your logic for delete record after each date. take date from server and write script for delete.
then go to cron tab in your cpanel and do settings for time interval to run cron and give path of your php script file.
MySQL doesn't have a task scheduler. So you have to use the task scheduler of your Operating System (CRON under Linux), or to lunch a basic task checker sub-script during the script of the main page (on another page that is supposed to display the changing data).

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.

How to schedule tasks in PHP ? (Storing & running techniques)

Hi all
I am using Zend framework for my PHP project. Basically I have few actions that I want to run automatically. I will be using a cron job to do the trick. The cron job will run a php script file.
Till now everything seems normal. Now I have created a table in my database and have stored the actions that I need to run in it. For example I need to do the following:
1-Create a sample file (5 times).
2-Upload all sample files (1 time).
3-send mail (continuous).
So I will store in my table the controller and action for (creating a sample file) and set its repeating time to 5 and set also its execution time. The same will be done for the other two actions.
Now the script file is run every minute. In the script file I will select all records that from the table that have an execution time equal to the current time then run them.
Now to make things just clear this system actually works but I was thinking about a better or improved scheduler techniques. Storing the scheduled actions in a table sound like a good idea but I was wondering if there was a superior approach ?!
This seems fine to me. You're using a cron job to run a PHP script which selects matching records to be executed from a database table. Storing data, as long as it's in a normalised form in the database, will be an efficient way. Monitor the load on your server and adjust from there if you need to.
My aproach is have a cyclic script that "empties" (set jobs as "finished") the table. And there are more CRONs with different timing that fill the table. So the workflow is like this:
minute: nothing to do
minute: nothing to do
hour: added export feed update
minute: export feed update finished
minute: nothing to do
minute: nothing to do
minute: nothing to do
minute: nothing to do
...
minute: nothing to do
five_hours: added "clear the cache"
minute: clear cache finished
minute: nothing to do
minute: nothing to do
...

Categories