Execute SQL query on a specific date - php

Its related to a website that I am developing, I want to deactivate all students who are being tutored, at the end of the semester. So, the user, on the admin side should be able to set the date when the semester is going to end and on that particular date, this query is to be executed and deactivate all students in the database. How do I do this using PHP and MySQL?

You can do time-based peridical runs with Cron Job.

Or you can build your login system to be aware of an end-date to their login. That way, you do not need to worry about a cron. But if you do a cron type job, build a daily task cron job so that it is something that you can use for additional tasks that are similar

Related

Triggering code at a certain time

Is it possible to tell PHP to execute a piece of code on a given date and time? For example, Blogger.com allows someone to set a blogpost to be published in the future (e.g. 12/12/14 6:00AM).
Can PHP do something similar?
(Sorry, I don't even know what the correct term for events like these would be to be able to even search for them! :( )
You can do this using a cron job (or scheduled task on Windows); although they are typically used for reoccurring jobs.
If you're using a database, most platforms come with a scheduler.
You can schedule your action in your database and use a cronjob on your server or use a cron job service To run your actions.
https://www.setcronjob.com/
For example when you want to publish your blog in the future, you save your publish date in the future and set some sort of auto-publish bit.
Then every hour a PHP script is ran by a cronjob, this script checks the database for all blogs which need to be published.
It's not possible to tell PHP to do this itself, since it would require a process to run forever to periodically call your PHP code. Thankfully though, there's a couple of things which do this:
1) Call a PHP script from a cron job, which then does any necessary work. If you don't have access to a crontab, you can periodically call this when a user pings your site instead, although that will be less reliable, of course.
2) Use at. This works in basically the same way as cron on Linux systems, but will schedule once and at an exact time.
for "triggering code at a certain time", cron works. But for something as simple as publishing an article at a specific time, it isn't needed. You can just store a publish date with your article. When displaying a list of articles you can adjust your query to something like WHERE PUBLISH_DATE <= NOW() and on the article page check if the article's publish date has passed before showing the article.
On Unix-like systems, there's Cron. You can manage Cron from PHP.
On Windows, there are scheduled tasks - you can also use PHP to manage scheduled tasks.
Be careful with this though - it's kinda hard to test, and you may end up with a schedule that cripples your server.

Recurring event in PHP

Hi there I'd like to make a recurring event based on calendar basis. The thing I want to ask is this: How to trigger the event? If the client doesn't login or open the webapp to trigger the event in the constructor, how do I make the code to run anyways.
I know cron jobs maybe the solution, but is there any other solution?(I want to avoid cron jobs).
You have to have something running in order for this to work; a cron is ideal for this, as it can run a script at regular intervals to check for things like events.
Without that, you are left by this being triggered by user action, but like you said, that user might not always log in to trigger it.
You could modify your code to have any user trigger actions for all users. It's a little more complex, as you'd need to allow for multiple users logging in at the same time when you only want one of them to trigger stuff. I would set up a table in your database to keep track of the last time things were triggered, and then if the gap since then and now is large enough when a user logs in, add a new entry in the table with the time now to prevent another user triggering this, and trigger the bits you need to run.
Like I said though, this won't be a perfect solution, and I really would recommend using cron. Is there any reason why you want to avoid using cron?
If that job should be triggered to keep the server in a good state, it should be run using a cron job, if its a database management, the database manager have the event's table to make cron jobs, if it's needed for only for the user, it can be a workload safe if you just don't do it until user login. You can manually login to trigger the event too, to an admin page maybe.

Scheduled Database Updates on Given Date - MySQL & PHP

I have a requirement where I need to Update some fields in database on monthly basis, I know I can use Cron Jobs, But I don't have access to use Cron, So is there any other way doing this in PHP, MySQL, JQuery or any other ??
I just need to update & Insert some rows in Database Tables on the scheduled time Automatically.
If you have site or something else with regular visits you can check with each visit is it time to run you job or not. If I not mistake wordpress's jobs are working in this way.
Have you ever heard of MySQL Event Scheduler?
Info
Creating event
Do you have access to it? If you want to check whether event_scheduler is running you can type show processlist - there should be a process run by User 'event_scheduler'. If not you can always run it.
Of course the best way is to add event_scheduler = ON in mysql config file (in mysqld section).

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).

Resent mysql values at certain time

I have a database with a table and some columns. One of these columns is flags (similar to SO) where users can flag comments. I would like to give each user 5 flags per day. So if a user uses 2 flags in a 24 hour period, the flags should reset to 5 at the end of the 24 hours. I really have no idea how to do this. Is there a special mysql function?
PHP:
$query=mysql_query("UPDATE users SET flags='5' WHERE userID='$user'");
how would i get this to repeat every 24 hours? (if this is the right solution)
The best way is probably to set up an automated task (possibly using cron) that runs a query to do this.
Make a script and add to cron job. It will automatically update all on specified time of day.
Cron is very simply a Linux module that allows you to run commands at predetermined times or intervals. In Windows, it’s called Scheduled Tasks. The name Cron is in fact derived from the same word from which we get the word chronology, which means order of time.
Using Cron, a developer can automate such tasks as mailing ezines that might be better sent during an off-hour, automatically updating stats, or the regeneration of static pages from dynamic sources. Systems administrators and Web hosts might want to generate quota reports on their clients, complete automatic credit card billing, or similar tasks. Cron has something for everyone!
You can use MySQL Event Scheduler and define an event to let it update every interval you want.

Categories