PHP MySQL – Once a Week Operations - php

I've got a database that processes an order - the Order is inserted at the beginning of the process but if the process is not complete (they left the page etc) the "confirmed" field is not filled in, this allows the admin to know which orders were complete or not.
At the moment I don't have the luxury of adjusting the way the website manages the data, I can however add some sort of weekly cleanout of these database fields where "confirmed" = 0.
How would I go about doing this automatically? Is there a way to run a query like this on a weekly, monthly, yearly basis?
Edit:
What if I added that the website is stored on a restricted, shared server where I have no access to the box itself?

You can use CRON to perform this job. But if you need to clean your database from incompleted orders only to show completed ones for humans and you don't export them to other places (e.g. manager visits backend once per day and looks for completed orders manually), i suggest you not to hire CRON for this. You can add few lines of code which will remove old entries from database each time backend was accessed.
DELETE queries are executed really fast so there will be no problems with perfomance. Real data deletion will be performed only by request. Also you don't need to have CRON (some cheap hosting providers do not give access to this service).

I usually create a PHP page that does the action and then schedule that to run using cURL and a CRON job.

If it's a Unix-like box, I'd put the cleanup in a PHP CLI script (that is, a PHP script that runs from the command line rather than in a web server module) and invoke that from a cronjob. Cron is a daemon for this exact purpose: Run things at predefined intervals. Cronjobs are defined in a crontab file, usually in /etc/crontab (for root) and ~/.crontab (for regular users); many distributions offer tools to safely edit this file.
Try man cron and man crontab for details. Also: http://en.wikipedia.org/wiki/Cron

setup a cron job and do a wget to run the script you want to automate. look at the link for more info http://www.phpmyvisites.us/faq/crontab-launch-automatically-statistics-archiving-process-every-62.html.

Write a short PHP script that updates/deletes the mysql records,
Then, if you have access to the linux/unix box, type crontab -e, you will then be prompted with the option to choose which editor you would like to view the current cronjobs, select the default and add this line:
0 0 * * 0 /usr/bin/php /path/to/script.php
Please check if /usr/bin/php exists before you set this up (you will need to install php-cli incase it does not exist, or the file might be also at /usr/sbin/php).
This cronjob will run once a week, if you want to change the frequency, use the manpages to learn how.
Also, are you sure you would like to run this script automatically? from what I understand you will need someone to first see the incomplete orders and only then to clean them up. if you run this cronjob you might be in a situation where someone did not complete his order but any evidence to that was deleted.
Hope this helps,

Related

Schedule update SQL Query using Php

I have an website with mobile phone prices and all prices are collected from external siites manually, all I want is to schedule update prices ($emag, $koyos)every 3 days.
My manuall update query is this:
$editare = "UPDATE modele SET koyos='$koyos', emag='$emag' WHERE id ='{$id}' ";
if ($dbh->query($editare))
{
print "<div><h2><img src=\"http://mysite.com/images/ok.png\"/><br /><br /Succes!</h2><br />
Vei fi redirectionat in cateva momente inspre adaugarea unui nou model de tableta sau telefon!
<meta http-equiv=\"refresh\" content=\"2; URL=index.php\"/>";
}
else mysql_error();
How can I do this query automatic at 3 days?
! My webhost not support cronjob SSH, cPanel or Plesk !
Firstly, you need to write a script in PHP that does what you want to do (basically, it's just your PHP code that you want to run at the scheduled time, no additions needed).
Then, using a cron job, you schedule this to run as often as you want.
*/4 * * * * wget --spider file.php in crontab runs every 4 minutes. You might want to read up on some crontab tutorials.
Here's an image from one of the links below describing how to schedule for the time you want:
Cronjobs are basically schedules that run every so often. They're really useful, and I use them all the time on Linux.
The link above is really useful for explaining what numbers you need to type to execute a script every x number of minutes, but you may need to google "Cronjob calculator" to automatically calculate the right numbers for you (I do this myself sometimes).
In linux, with crontab installed, type crontab -l to see a list of all cronjobs. Type crontab -e to edit your cronjob list for the current user. This is where you paste the code I gave you above.
Edit: To get your head around cronjobs at a basic level, first install crontab. Then, use the code above to execute a script that writes to a file. Then, just tail -f that file, and watch how (in four minutes after the cronjob starts) it updates in front of your eyes.
Start with the basics. Then get it to execute your MySQL script.
Useful Links:
http://www.linuxweblog.com/crotab-tutorial
http://clickmojo.com/code/cron-tutorial.html (this looks good)
http://www.tutorial5.com/content/view/95/51/
http://www.thegeekstuff.com/2011/07/cron-every-5-minutes/ (minutes / days explanation)
Note: You can't do less than a minute. The last link suggests a sleep() command to get around this, so check that out.
The problem with OP's question is that because he uses very restricted shared hosting, he doesn't have access to SSH, cPanel, cronjobs or anything else like that.
An option here is to spin off a separate process for wgetting the PHP file using exec(), or shell_exec(), with nohup and an ampersand (&) at the end, along with a long sleep() time for the number of days the OP wants. This will cause the PHP script to run in the background on the server.
Safeguards need to be put in place with the above example, ie: if the server is rebooted then the script will no longer be running and will have to be restarted. Conversely, if the user accidentally hits the script at the wrong time (or someone else does) then multiple processes will be spawned, so a safeguard to check whether the process is already running for example could occur (or a basic password entry requirement may help to avoid accidental hits).

Creating PHP Bots for site maintenance

I have a PHP website in which, when a member visits a page, a series of database maintenance actions are made.
For example, in a common page, I've included a PHP script which checks how many posts every user has made and updates the database giving them points accordingly.
The problem with this method is that my website has 100+ members, and I'm worried that my scripts start to slow down as my memberbase grows.
Is there any way to code a bot in PHP, so my database can be updated without the user's intervention?
You should run a PHP file from within a cron job. Most PHP hosts including shared hosting provide cron access.
With cron you can schedule a task to run on an interval basis. This PHP program will then go through and do the updating that you require. So... take the code you do now and move it into a seperate PHP file and then tell cron to run it maybe once an hour or whatever you deem to be the correct interval.
For best performance, you need to update users table when he publish the post, not every time when need to know how many posts he published.
Create a cron to run daily (for instance) with the follow command:
php -q /home/cpaneluser/cron.php
And put a cron.php outside of public_html with all maintenance taks.
Or allow only administrators to do the maintenance tasks with a link in administrative panel.

php robot for maintainance of db?

i want to have an automatic maintainance of my mysql database.
eg deletes all old threads, users and checks if a user hasnt been active (ive set up that javascript is updating a status column in mysql) for over 60 min, i set the persons status as logged out in mysql. stuff like that.
now how should i do this part. should i write this code in a php file and then fire it up with Firefox and let it be opened all the time.
is this the right method? any other suggestions are welcomed
Write your maintenance script in PHP and schedule it to run on a regular basis using cron. Don't rely on a desktop browser window.
The PHP interpreter can be called straight from the command line, or in your case, a cron job.
Just be aware that called this way, some of the automatic GLOBAL variables won't be set (there's no REQUEST, SERVER, etc) but you most likely don't need those for your task anyway. Be further aware, though, that when you call a task from a cron job, many of the environment variables you expect to be set up in your command line, aren't. That's because cron shells don't process your login scripts.

How to send on a specific day in php?

I want to a e-cards or something like that. The user can choose the e-cards, after chosen, he must enter the some fields like name(to and from), email(to and from), message and I want to let user to choose which date to send the e-cards.
How to send the e-cards on specific day? I need to write a script that run every new day? How to do that? Sorry, I am new to php... (but not beginner like not even know how to execute mysql query, get message from url etc)
Yes, you need a script that runs every day. (Barring ridiculous maneuvers like trying to fake this by checking on Web requests.) The usual way to do this in a Unix context is called a cron job; if your hosting provider is Unix-based, you should look into what they provide for making cron jobs available to you. On Windows there's a parallel service called Scheduled Tasks.
A similar question was disscussed here resetting-a-mysql-field-value-without-user-execution
I'll just reiterate: There are web based cron services too. This could come in handy if you only got a shared hosting plan and can't add cron jobs. They will call an URL at a regular interval that you can set. Usually very cheap. (Cheaper than upgrading to a root-access server anyway.)
Just search Google for web based cron
ciao!
/0
This is for *nix.
Let's say you have a php script that sends email on a specific day called mailer.php
<?php
//mailer.php
if (date("m/d/Y") == "06/02/2009") {
mail("client#email", "Subject", "Body");
}
?>
We are going to assume that you already have cron daemon running in the background.
If you have root access to your machine, then setting up a cron job is simple as editing a file.
Open up /etc/crontab file and add the following task:
1 14 * * * root php /path/to/your/scrip/mailer.php
This means, as a root, the mailer.php script will be running daily at 02:01PM. You can change the numbers to whatever you desire.
This is somehow complex. First it depends on your system. If it is Linux/BSD/Unix/Solaris then you have this handy utility as cron. If you are using Windows, you have Scheduled Tasks. Run your script daily (or as you wish) and check what cards you have to send today.

Resetting a MySQL Field value without user execution

I need to reset a MySQL Field value automatically at midnight. It is a specific column in a specific row in a table. I know how to do this in PHP but I do not know how to execute the PHP Script at midnight without someone having to do it themselves. Do you have any viable solutions?
Edit:--------------------
Preferably without using Cron Jobs.
If you are running on linux you would use a cronjob
On most distros there is a command called crontab that schedules tasks for you and a specific format you need:
0 0 * * * php /path/to/file.php
EDIT:
Wrote this before you edited yours :p I'm not sure there is any other way. Do you have a specific reason not to use a cronjob?
You may not even need to specify the php part if the php file is marked as executable i.e
0 0 * * * /path/to/file.php
Just do "chmod +x /path/to/file.php" form the linux command line
There are web based cron services too. Basically you set up an account and then they visit an URL of your choosing at a regular interval. On your server you set up a page that does what you need to get done. Preferably give it a unlikely-to-find-name like myjob789273634ahhh8s2nhw8sghusgf874wfu.php. You get the idea. (Remember that PHP-scripts timeout after like 30secs.)
Here's a google search:
**oops I'm new so I can't post URL apparently. Just search for "web based cron".
Good luck
/0
You could write a job scheduler into your program that runs jobs in a cron-like way. It would require a user to interact with the system to trigger, but it might be good enough depending on your needs. This is much more complicated than just running a cronjob, and does not ensure prefect timing (since it wont run until a user hits a page).
You'd probably need to add a table into you database that would list the job, the time you want them done, and a locking flag to avoid concurrent attempts to run the job. Each time your script runs, you'd check this table for overdue jobs and run them as needed.
Asking how to reliably set off a script at the same time every night without cron (or a scheduled task, on Windows) is like asking how to make a dynamic website without a server-side language.
If your app absolutely relies on a script running exactly at midnight, cron is a requirement. If your users have a hosting company that (stupidly) does not permit cron, they're going to be out of luck.

Categories