Execute sql query by cron job - php

I need to reset all my documents number in my magento each. My hosting is limiting the use of event scheduler with my actual plan, so I'm asking if exist a way to execute a query on my db using a php file and a cron job executing each year at midnight. I think it needed also to store my db admin password. It is secure to create a php script with db password inside? Or there's another way to make that?

Related

Update database table cyclically every 5 minutes

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.

do php file at a time according to database

I'm creating a website and in my website I have a MySQL database. In the database there is a table that has two property "date" and "time". I want to run a php code on a date and time according to records of table, automatically. How can I do it?
I see three options, listed here in order of difficulty to implement, easiest to hardest.
Create a script that is scheduled to execute every now and then (exact timing would depend on your needs for accuracy), which checks the database to see if there are any items in there that should have been executed in the time span since it's last execution. If there are, execute them.
Write a daemon application/script to monitor your database, and execute the tasks when needed. This one is not all that different from #1 except that it wouldn't require scheduling or manual triggering. That could be handled within the application/script itself.
Create a script that checks the database for changes, and schedules/reschedules/removes any tasks accordingly. This script would then be scheduled to execute every now and then and/or be executed manually by the application that manages the database.
Write a php script (that queries the database and runs the script) powered by crontab (for periodically running your 'query'-script) in a shell (on linux)

Truncate database automatically without event scheduler?

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.

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

Execute SQL query on a specific date

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

Categories