How to update data in MySQL periodically? [duplicate] - php

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Is there an equivalent to cron in Windows?
How do I update my data in MySQL periodically?
Say I have table. One column is "Date". Another column is "Expiry_Status".
How do I update the table periodically(a few hours) such that, if the date in the "Date" column is after the current date, the "Expiry_Status" will be updated to "Yes" value.
I am using WAMP as the framework.
Edit: Thanks for the answers but a lot of you guys gave answers about changing the database architecture. The case I gave above is just a simple example. I am more interested on how databases are updated regularly. ie: the program used, the syntax, the paradigms....etc etc.
Thanks again.

You write a program to perform the update, and then run it using a schedule system.
On Windows (since you tagged the question WAMP), this is usually done with Task Scheduler.
Other operating systems usually use cron.
Changing a status column is redundant though. You can search for data where the expiry time is greater than NOW() instead of where the status is Yes. This sort of scheduled script is normally used when the old data is being deleted.

write a php script which checks times and updates appropriately then use windows task scheduler to run them at certain interval. (assuming windows os...)

What value does having this expiry_status even serve? If you have an index in that date/datetime field (which you would need to effectively update the expiry_status field anyway), you can just as easily query on...
WHERE `date` > NOW()
...in order to determine active records. That is in essence all you are going to be doing with this proposed update query anyway. Why maintain an extra field if you don't need to?

Related

How to create a script that will run automatically every one hour and sends a curl request? [duplicate]

This question already has answers here:
How to set up a cron job to run an executable every hour?
(7 answers)
Closed 3 years ago.
I want to populate a hostoric database installed on a server. To do that I have to send multiple (curl)http requests on an endpoint changing the value of a certain field of a certain entity, also saved on a diffferent database. The problem is that I need to change the value of the field every hour for many days. I get it that people use cron jobs for this kind of task, but I need to know firstly in what language should I write the script preferrably(php, python, etc-with php being my personal preferred choice), or it does not simply matter and the important thing is to execute it via cron? Also what would the syntax for cron be in my scenario where I need to change the value every hour for say seven days?
Hi there are a lot of possibility to do cronjob, depends of your infrastructure. For example:
in a web-hosting with cpanel/plesk you can call a script (php for example)
you can also do cron by using user a starter, e.g. by creating a table on db with last execution on every user you can check the last execution and comparing it with actual time you can decide if start or not a scripts
you can create a crontab for your server in caso of managed server or vps (e.g. https://crontab-generator.org/)
you can use an online cron service. e.g. https://www.easycron.com/
and so on...

Code running every 24hours that updates data in database

I want to ask how can I do something like this.
I want every 24 hours to update the value in the table in the database
and I donĀ“t know how it can be done.
I need to it to be automatic, that means I won't be doing anything it will just do it on itself when its time
I am using MySQL database with
PHP version is 7.0
So how it can be done?
I don't need code (but I don't mind) but the mechanism, I want just description can I put some code into the database and it will do it automatically or run some file on server or?

How often can I query MySQL database through PHP [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have a microcontroller which reads temperature from a sensor every second and serially sends it to a computer attached to it via USB. The computer has a LAMP server running on it. It takes the temperature measurements from the microcontroller and adds them to a MySQL server running on the computer. There is also a PHP file on the computer which is able to read the most recent temperature from the database and "echo" it. I have an Android app which queries the PHP file to get the temperature and update it on the screen.
Now, the question is: All of this process is happening on a local area network, so it's pretty fast. Would it be a good idea to "poll" the server every second if this was happening over the internet? How often should I ideally query the PHP file if I was doing this over the internet?
It just depends.
You can query your database as often as you can, however you may meet efficiency issue.You can just write a script which just query your database and set a clock to make that script just run for sometime (30 seconds for example). You count the total query, and output the count into file.
Use this method you can test the max qps of your database with that specific query.
Normally, you should reduce the frequency of every database query, cause the database query may be the bottleneck of your script speed.
You can implement some asynchronous method to update it. Although there is no limitation on how many times you can query but it can hamper your server performance if too many connections are open. So you have to think of some logic or time which will suit your need (get updated data to user) and also not hamper server performance. You can also lower the burden by just querying updated data not entire data (as mentioned by #Rikesh)
Create a updateTime field in your table. Each time you update the row update this field with current time. When you are querying use this field. For the first time the updateTime = 0. store the max updateTime values and use it for the next call. So that each time you are fetching rows that are updated after the given time.
Another technique was cache the fetched values and make a query to fetch the data that are updated after the cache created. And sync the cached data with the updates.
If you query and fetch all data often( 1 sec as you mentioned in question), it will affect the server performance.
Use a cron job e.g set the script to run every so and so minutes...or you could use ajax to call your script every second or on events such as button clicks. If what you are aiming for is a real-time app you might want to look into some real-time technologies such as comet long-polling(facebook notifications), websockets, xmpp or as I said ajax!
If you want to show/get the only update values and reduce the time then you have to compare/check the id value with the previous one,it means
save the current retrieved id or any unique value from row(like datetime)
and for the next time check the id or datetime values (like select * from table where datetime>currendatetime)so that you can get only update rows

Automatically execute MySQL query or PHP script without cron

So, I have a forum and I want to reward users who donated automatically with a special membergoup, I can do this by adding it into forum's MySQL database. What I need now is to remove such usergroup when a month has passed, automatically. What I would like to know is if it would be possible to make it purely on PHP for example, or another similar alternative.
Thanks in advance
php has no job scheduling mechanism built into it, unless you run a script as a daemon. MySQL has its own scheduling mechanism: http://dev.mysql.com/doc/refman/5.1/en/events-overview.html
You could store the date that the donation was made, and then, you could check the difference between that date and the current date.
If the result is > month then the user should not be in that membergroup.
Also, to avoid calculating that difference every single time, you could have an extra field in your database for each user, which would indicate if that user should be considered a donator.

When do I need to use cron?

Lets say there is a thread ( on forums ) which will be active for 3 days only. Now, after 3 days, I want this thread automatically closed.
Can I use reference to time when this thread is created in database, and than make if statement if current date + days is bigger than date created, I will print out "<h2>Thread Closed for posting</h2>"
And when I consider some other tasks, I suppose I can use reference to time and have certain event executed on this.
Am I right?
You can use a cron (ran every minute) to set a status field on the thread table to closed such as.
UPDATE threads
SET status='closed'
WHERE lastPost+INTERVAL 3 DAY<NOW()
Then in PHP something such as
if($thread['status'] == 'closed')
{
// Put your HTML here.
}
A 'cron' is a task that runs at a specific interval or time. This means that it should be used for tasks that must be done without user interaction. For example, a backup, automated emails or pulling data from a remote service.
What you want is better suited to a condition on checking whether a thread is closed. Rather than just having a flag, you also check the age. This means you can change your old-thread logic without needing to edit the database.
You could make a PHP script that gets executed by cron (read up on how to execute PHP in the command line) that SELECTs all the posts in a certain date and then sets them to closed. If you ran that, say, twice a day, then you could do a good job in getting all those posts closed.
Good reference on using cron to run PHP

Categories