Way to auto update a column in database depending on time - php

Is there any way or script that allows to auto update the column in my database everyday at midnight 12.For eg I have a column 'x' ,currently value in certain row under column 'x' is say 10, What I want is to reinitialize that value to 0 as the clock strikes 00:00:00.Is there any way to do this ?
My database details : mysql in phpmyadmin.
Scripts I am aware of : javascripts or php.

You can use MySQL's event scheduler
CREATE EVENT update_status ON SCHEDULE EVERY 1 DAY STARTS '2013-11-02 00:00:00' DO
UPDATE your_table
SET x = 0
In order to make the event scheduler run you can add this in my.cnf:
[mysqld]
...
event_scheduler=ON
Read this for more info http://dev.mysql.com/doc/refman/5.1/en/create-event.html

You seriously need to look on CRON jobs. This CRON Job runs every midnight.
0 0 * * * php /var/www/yourscript.php
yourscript.php
<?php
//... make conn to db
//make use of query something like this
mysqli_query("UPDATE yourtable SET yourcolumn = 0");

CREATE EVENT event_scheduler_x
ON SCHEDULE EVERY 1 DAY
STARTS '2013-11-04 00:00:00'
DO UPDATE tbl
SET x = 0;

Related

How to write an event scheduler that run every midnight ? And set a value to 0

I have written an Event Scheduler in MySQL I don't why it's not working.
I would like to set the reserved = 0. the event should run every midnight at 00:01:00 european time. when the expiration date (exp_date) is less then current date time here is my script.
CREATE EVENT IF NOT EXISTS disable_reserved_value_of_prospect
ON SCHEDULE
EVERY 1 DAY
STARTS '2022-03-30 00:01:00'
ON COMPLETION PRESERVE
ENABLE
DO
UPDATE prospect
SET reserved = 0
WHERE exp_date < CURRENT_TIMESTAMP;
But my script doesn't work I don't know why ?
Can we use any if condition inside event script?
Thanks in advance
ON COMPLETION PRESERVE is for one-time event only,so you don't need it in your recurring event. Try modifying the code like below:
delimiter //
drop event if exists disable_reserved_value_of_prospect //
CREATE EVENT disable_reserved_value_of_prospect
ON SCHEDULE
EVERY 1 DAY
STARTS '2022-03-30 00:01:00' DO
BEGIN
UPDATE prospect
SET reserved = 0
WHERE exp_date < CURRENT_TIMESTAMP;
END //
To verify, we can reduce the 1st kick-in time by using EVERY 1 minute STARTS now(). If nothing happens, there is probably something wrong with the UPDATE statement. Substitute it with a TEST statement to insert a line into a test table or something.

I want to automatically update my sql table data after 20 days from stored date

enter image description here
I have this table in My SQl. I wants to change the column "Status" from 'Pending' to 'Defaulter' after 20 days from "rec_date" column automatically.
How can I do this?
Let's answer it in a generic way, to create a cronjob in an easy way.
Create .php file (ex.php)
Do what ever the logic, you want here like select from the db the records where "rec_date" > 20
Update the database status
to run the php file, simply from terminal type:
php ex.php
You need to run this php ex.php in a cronjob (on server, or locally) and specify the time like every 5mins run the ex.php file
Create a proc and Run it on schedule
CREATE PROC ProcNameHere
AS
BEGIN
DECLARE #datetime DATETIME
SET #datetime = CURDATE()
UPDATE tablename
SET [Status] = 'Defaulter'
WHERE rec_date < DATEADD(DAY, -20,#datetime)
AND [Status] <> 'Defaulter'
END
You can use trigger and event scheduler which initiates after 20 days

Adding +1 to a mysql value every day

hello I have a table called 'members' for my website/php code and I dont know how to increase a certain value by 1 everytime a new day starts (or eventually everyday the user logs in the first time).
Can anyone help me?
-thanks.
You can do this in either where you process the user login, or in a file that's scheduled with a cron job:
For everyone:
UPDATE members SET field = field + 1;
For one person
UPDATE members SET field = field + 1 WHERE user_id = 42
If anyone finds this through searching or what not you could do something like the following:
SET GLOBAL event_scheduler = ON; -- enable event scheduler.
SELECT ##event_scheduler; -- check whether event scheduler is ON/OFF
CREATE EVENT e_store_ts -- create your event
ON SCHEDULE
EVERY 24 HOURS -- run every 24 hours
DO
UPDATE myschema.youtable set mycolumn='1'
Source: https://dba.stackexchange.com/questions/43293/updating-a-column-after-expiration-of-specific-time-in-mysql

How to increment mysql data after a fixed time slot automatically?

For example, I am creating a student management system and I want to increase the student standard after every year. If student is admitted in 1st standard then next year he would be in 2nd after that 3rd and so on.. Please help!
you can use MySQL EVENT like
CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 YEAR
DO
UPDATE `student` SET `standard` = `standard` + 1
WHERE some_ID_Column = value;
Set up a cron job that runs nightly and run a stored procedure that checks if the students have reached the condition for the next standard and if they have update the records.

MySQL - How to decrement int value every 24 hours?

I am working a system in PHP. In my MySQL have a table borrow. Inside borrow is borrow_status a varchar and borrow_remain which is an int value. When the user click the submit button. The query will run something like
INSERT INTO `borrow`( `borrow_status`,`borrow_remain`) VALUES ('Borrowing',3)
How can I decrement the borrow_remain value per 24 hours then UPDATE the borrow_status to Finish when it reaches zero?
EDIT: I also have a book table, that have a book_quantity, my only concern is that the book_quantity must increment if the borrow_status is updated to Finish.
You can implement the above scenario following the three steps given below :
1) First you have to enable Mysql event scheduler.
SET GLOBAL event_scheduler = 1;
2) Create a procedure in mysql that will do the borrow status update related functionalities.
delimiter //
CREATE PROCEDURE UBS()
BEGIN
UPDATE borrow set borrow_remain = borrow_remain - 1
WHERE borrow_status='borrowing';
UPDATE borrow SET borrow_status = 'finished'
WHERE borrow_remain=0;
END //
delimiter ;
3) Create an event which will be scheduled in 24 hours interval.
CREATE
EVENT UBSEvent
ON SCHEDULE EVERY 24 HOUR STARTS '2015-03-04 00:00:00'
ON COMPLETION PRESERVE
ENABLE
DO
CALL UBS();
Note :
UBS is the procedure name.[ UBS = Update Borrow Status (just a
relevant abbreviation ]
The event will start from tomorrow (4 March 2015) at 12:00 AM
Have a nice day!
EDIT: To increment book_count in book table whenever a borrow_status in borrow table gets updated to 'finished' you need a trigger.
Suppose you have a book table where the count of the book is denoted by book_count; Then the following trigger will do what you want.
DELIMITER $$
CREATE TRIGGER IncBookCount AFTER UPDATE ON borrow
FOR EACH ROW
BEGIN
IF NEW.borrow_status = 'finished' THEN
UPDATE book SET book_count = book_count+1;
END IF;
END $$
DELIMITER;
SUGGESTION : You need to improve your database design. Otherwise you might get more troubles in designing a complex architecture in future. Stay well!
Have you look at Events in mySQL? You can create an event and set it to run on a particular schedule. That might be the ticket for your problem!
You need to have mysql version 5.5 to use event. May be your desired event looks like this:
CREATE EVENT update_borrow
ON SCHEDULE EVERY 24 HOUR
STARTS CURRENT_TIMESTAMP
DO
BEGIN
UPDATE borrow SET borrow_remain = borrow_remain - 1 WHERE borrow_remain > 0;
UPDATE borrow SET borrow_status = 'FINISH' WHERE borrow_remain = 0;
END
You could also set a cronjob that executes a piece of code to decrement "borrow_remain" and updates "borrow_status" accordingly. If your hosting has cPanel it's pretty easy: https://documentation.cpanel.net/display/ALD/Cron+Jobs

Categories