how to use PHP to automatically delete from mysql at set date - mysql

I have added some rows into my ad table in the database, their properties includes created date and expiry date.
I need php script to automatically delete the row that its expiry date has reached.
Pls assist my project.
Thanks u

You could use MySQL's event scheduler to automatically delete such records when they expire:
CREATE EVENT delete_expired_101
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 3 WEEK DO
DELETE FROM my_table WHERE id = 101;
Or to run an automatic purge of all expired records on a regular basis:
CREATE EVENT delete_all_expired
ON SCHEDULE EVERY HOUR DO
DELETE FROM my_table WHERE expiry < NOW();

<?php
$today = getdate();
if($today <= $expirayDate)
{
//your deletion logic
}
?>
The other way would be to filter your select query i-e hide all those fields which aren't expire;
$query = "SELECT * FROM `yourTable` WHERE `expiration` >= NOW();";

What you are looking for is crontab.
Using it, you will be able to schedule scripts to run in determinate times, for example:
Run deleteExpiredRows.php every 5 minutes
Run updateOnlineUsers.php every 2 hours
Cron is very easy to use. Check this quick reference guide
Setting up crontabs at your web hosting:
/path/to/hosting/php /home/user/public_html/script.php
Path to php may vary, so contact your hosting to get this information. More information about crontab can be found here at stackoverflow.
Note: you might want to check if your web hosting service supports crontab.
After this is done, you just need to create your PHP script to delete every row older than current time.

Related

Perform delete a perticular records after some interval of time in mysql

DROP EVENT `deleteTestEntries`;
CREATE DEFINER=`root`#`localhost` EVENT `deleteTestEntries`
ON SCHEDULE EVERY 1 MINUTE
STARTS '2018-05-25 18:17:01'
ON COMPLETION NOT PRESERVE ENABLE DO
DELETE FROM lead_master WHERE lead_master.lname LIKE '%Test%'
What is wrong in above event.
It has been created with no errors but not performing any action.
I simply want to delete the records from my lead_master table where lname is 'Test'
Go into my.ini file and add this line,most probably this is the issue.
event_scheduler = on
Restart mysql.Apparently you can even set it on the fly
I finally did it with cron jobs.
I created a controller which calls a model function contains query to delete record.
and set the cron job which calls the controller after specific interval of time.
I am guessing that you don't need an event every minute. Just define a view to filter out the records you don't want:
create view v_lead_master as
select lm.*
from lead_master lm
where lm.lname not like '%Test%';
Then schedule a job or event every day at a slow period to delete the rows you want. Your application should use the view. Your testing should use the base table.

Trigger that delete row if currentdatetime > datetime+5

I'm creating a database where i want to create a trigger.
This trigger will delete a row if the current time is 5 min over the requesttime in the table.
The requesttime is a datetime attribute in the table.
How can i make a trigger which do this?
You don't want to do this. First, triggers only run when something is happening inside the database (such as an insert or update). They are not the same as scheduled jobs. Second, you generally don't want to do this in a scheduled job. Instead, just create a view:
create view v_table as
select t.*
from table t
where requesttime >= now() - interval 5 minute;
This will return valid requests.
Then, at your leisure, delete the old rows -- if you really need to. There is a good chance you might want to keep them around to know what happened in the past. But you can delete them on Sunday morning at 3:00 a.m. or some other slow time, if you want.
You may want to consider Event Scheduler added in MySQL 5.1.6. Events can be seen as an alternative to Cronjob.
Their main advantages are:
Platform independent; directly written in MySsql
Ability to list all events SELECT * FROM INFORMATION_SCHEMA.EVENTS;
Built in error logging option
Syntax is something similar to:
CREATE EVENT my_event
ON SCHEDULE
EVERY 1 MINUTE
STARTS '2015-01-01 00:15:00'
COMMENT 'Removes forgotten password tokens older thank 1 week.'
DO
DELETE FROM my_table WHERE some_timestamp > NOW();

Triggers in MySQL

I have database of Real Estate. In that I want to delete a record after particular time event say 22 days. I want to write trigger that will delete record from table automatically after 22 days.
Something like this:
CREATE EVENT CheckDeletes
ON SCHEDULE
EVERY 1 DAY
DO
DELETE FROM YourTable WHERE DATEDIFF(NOW(), YourField) > 22
http://dev.mysql.com/doc/refman/5.1/en/create-event.html
Good luck.
Triggers fire immediately based on some particular action in the database. What you will probably need to to do is to create a script that you can run at scheduled intervals (via cron or similar) in order to look for records older than 22 days or whatever and delete them.

cron job/mysql event task

I'm completely clueless as to do a cron job/mysql event task for some things in my database to be updated automatically every 24 hours.
Basically here's the 2 queries I need to be ran every 24 hours as soon as users_stats DailyRespectPoints hits '0'.
UPDATE user_stats SET DailyPetRespectPoints = '3' and
UPDATE user_stats SET DailyRespectPoints = '3'
Those are what needs to be ran so that when someone uses all 3 of their DailyRespectPoints, it refills back to 3 in 24 hours.
Thanks!
you could create trigger , which will be executed automatically when DailyRespectPoints hits '0'.
you can read more about the triggers here http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
or you can write more details about your database/query-s and we can help you write the trigger
First write a script that will perform the work you need done. Based on your question, you will check some table value, then execute your two UPDATE steps.
After your script is tested and performs correctly, you can schedule it to run every day at some point in time using cron (probably crontab).

how to increase a field value of mysql table every some minutes automatically

I have a field in mysql table that I want increase this field value every about 5 min (298 seconds) ,
how we could increase this field value?
thanks
If you are using MySql version 5.1.6 or above, you can use the built in event scheduler:
CREATE EVENT e_update ON SCHEDULE EVERY 298 SECOND DO
UPDATE tableName SET Col = Col + 1;
Or
CREATE EVENT e_update ON SCHEDULE EVERY 5 MINUTE DO
UPDATE tableName SET Col = Col + 1;
You could make a php script that updates your database field however you like, then create a cron job that executes this script every five minutes.
Replace PHP with your scripting language of choice.
One way to do this is to have a php script on your server, called by cron every 5 mins.
*/5 * * * * /home/whatever/updateField.php