So I have a game which has a scoreboard based on weekly scores. At the end of the week I would like it to automatically delete all the entries and cache new values again. Issue is I don't know how to trigger such an even via PHP! Any help?
Sounds like you should set up a cron job.
It seems you have unusual database design.
I know no site (gaming or not) that erases weekly data.
The purpose of database is to hold data. Why not to let it just do it's job?
Create a small php file which connects to mysql and TRUNCATES the table ( better and faster to truncate instead of delete all ).
And add this to your crontab.
0 0 * * 0 PATH_TO_PHP myFile.php
Here is a crontab tutorial :
http://clickmojo.com/code/cron-tutorial.html
Generally these type of activities are carried out using Cron Jobs or Task Schedulers which will run periodically to perform the action you would specify.
I think the easiest way would be creating a PHP script which deletes everything on the table, then creating a crontab job for it.
Related
I have a database that has 20 rows each row I had set a Boolean value to it, so it is by default zero and when a row gets viewed its value changes to 1
I want the database to send any kind of signal that when 10 rows their value change from zero to 1, a certain PHP file fires up and starts a process that will affect only these 10 rows
How can I do that?
Thanks in advance
I would say, query from the php file every set amount of time to your database
The other way, database to execute a php file is almost impossible.
If you are using mySQL as database, a trigger could invoke the sys_exec() UDF available here: https://github.com/mysqludf/lib_mysqludf_sys#readme
So, there might be a possibility, actually, via an UDF function that would launch the php executable/script; not that easy, but seems possible ;-)
Invoking php from mysql is impossible, all you can do is set cron jobs for it. Cron job check mysql after certain interval of time and run the respected code
Every database is only a storage and it is its purpose in the system. Don't try to trigger any external process by the storage. The communication with the storage should be only a one way.
Rather think how to trigger your process from outside. Generally, there are two approaches:
a script that will check your database data in some interval like 1s, 10s, 1min or whatever would fit for a particular process
the current process that is updating your data can check your data and trigger another process if needed.
You can not trigger external file/script from mysql.
What you can do is create a cron job which run after certain interval of time which check database and perform certain operations.
I have to populate and update one of my MySql database table using a complex and expensive query, based on selection from other table's data. This table doesn't need to be always fully updated when i make a query on it, but i'd like to have a cyclic update every 5 minutes.
This automatic update should be infinite and i need to be sure that it never stops.
After some research, i've found some solution, but i don't know which is better for security and performance.
One of these could be my goal:
Don't create table and make complex query from php every time to get the desired result
Create a php script that repeats cyclically and update table db, maybe using Cron Job.
Update table using a sql event
I think that first solution could be to expensive since query is complex and there could be many request every second, but the result is always updated. I don't have experience about Cron Job, so i can't know if it could be a good idea or not. For the third solution, i still don't have database privileges to run events, but i'd like to know if it could be a valid solution.
All other solutions are welcome, thanks.
Do not use cron. Think about what will happen if one instance goes beyond 5 minutes and the next starts up. Eventually you will have hundreds of copies bogged down stumbling over each other.
Instead have a single job in a loop doing the update. (OK, you could have a cron job to perform a "keep-alive" task of restarting the query if it dies.)
The job would
CREATE TABLE new ...
INSERT INTO new SELECT complex-stuff...
RENAME TABLE real TO old, new TO real;
DROP TABLE old;
loop.
I would opt for Cron Job.
It doesn't clog any request, since it's executed from the operating system.
You can define which user executes the script (cron -u apache -e).
Easy to define interval. (i.e. every 5 minutes */5 * * * * php /path/to/script.php).
It's loggable.
Additional Notes
I had a cron job running under root and it worked just fine. My problem was that the project had a private logging mechanism that each log file would be created by apache user. By running it from root, sometimes the file would be created by root and after that, the scripts being executed by apache would not be able to APPEND to the log.
I also had an emailing script that would run once every 2 minutes that got stuck for 1h. Turns out, because of a bug in the application, an invalid email address (somethingwithoutatsign.com) was inserted into the database, which made the PHPMailer library throws errors. After that, I added a catch block that would send an email to me whenever an exception was thrown. Now, if the script stops running because of bad execution, I get to know right away.
My host (blueangelhost.com) claims that I can't use the event scheduler because it takes up too many resources. I have access to cron jobs in cPanel, but I've tried and they don't seem to work.
So, my question: Is there any kind of efficient PHP code that will automatically truncate a MySQL table in a database?
Well, if it needs to be automatic, or at a specific time, not really. But you could have your website trigger the script when someone gets on it, here's the approach you could use:
On a script that is run on every page (header, menu,footer, layout):
Check in DB or file, the date of the last truncate;
If the date is yesterday, run the truncate;
Change DB or file and put current date;
This way, it will execute once a day. But never at the same time, and not if no one walks on your website for a whole day.
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
Hi I have a MySQL database table where a user inputs messages (via a form) and I was wondering if there is a way to automatically delete a message after say 1 minute has passed?
The code i'm using is PHP.
Thanks very, very much for any replies :)
Well, you could always run a delete query before you do anything else. For example whenever you check for messages, first delete all messages older than 1 minute.
I think I would rather just not get the messages older than 1 minute though. It can be nice with a log :)
I can think of two ways to achieve that.
Make a script to run every X minutes and make any changes in your db.
Delete all expired records before or after inserting new records in your script.
When a message is inserted to the DB, store a time_created timestamp. Then in your PHP, you only display messages whose timestamp falls within 1 minute of the current time.
Use a cron job to purge old messages.
You could make a cron script to run every minute and delete all old messages
or make a valid_until column in your table, and set it to NOW()+60 - then only show rows which have valid_until >= NOW().
Building a cron service is the first thing that pops into my mind, although is probably an unnecessary complication.
You can call the delete in the same script that does the insert after a sleep of 1 minute.
sleep ($seconds);
// call the delete query
Another way is to pass the delay logic to a Mysql trigger that will do the delete for you.
SELECT SLEEP(<seconds>);
Solution 1: user cron job which runs every minute to check and delete
Solution 2: use ajax do that job
I personally suggest the first solution but if u have no SSH access to the server u have to choose the second one :D
thanks for all the replies/help :) the first suggestion by Svish 'You could always run a delete query before you do anything else. For example whenever you check for messages, first delete all messages older than 1 minute.' will work...so obvious don't know why i didn't think of that doh! Thanks again every1.