0

I'd like to automatically execute the following PHP script to autmoatically delete a row in table after 3 days. However, the script seems not working, even though the query already yield the correct result. I've tried to run it manually, and it also worked. I'm not sure whether I've written the script correctly, but here's the code:

<?php
require_once('../../resources/db_connect.php');
$query = "DELETE FROM user_orders WHERE date_order < DATE_SUB(CURDATE(), INTERVAL 3 DAY) AND ID_status_order = 1";
$result = @mysql_query($query); 
?>

I'm using xampp on Windows. I've tried to execute it via Windows Task Scheduler, but I don't think it's working. Any other idea?

Cheers

gaban
  • 55
  • 1
  • 5
  • try to remove the @ before `mysql_query` and run the script manually to see the errors – SERPRO Mar 06 '12 at 10:22
  • Welcome to Stack Overflow! You are not doing any error checking in your query, so it's no wonder your script will break if the query fails. How to do this is outlined in the [manual on `mysql_query()`](http://php.net/mysql_query) or in this [reference question.](http://stackoverflow.com/questions/6198104/reference-what-is-a-perfect-code-sample-using-the-mysql-extension) – Pekka Mar 06 '12 at 10:22
  • remove @ and you'll able to notice the errors. – Kuba Mar 06 '12 at 10:23
  • any idea how to execute the script manually? – gaban Mar 06 '12 at 10:29
  • okay, so my script is actually working. I tested it by putting it on the index.php page so that it automatically executed. Now, I want to automate it on my xampp in my Windows machine. Any idea how? – gaban Mar 06 '12 at 10:43

3 Answers3

0

Hidding errors are not good enough. Thry and remove the @ first and see what errors you are having. Come back and give the errors.

  • didn't get any error. thanks for the tip mate. I think now I need to know how to run this script automatically in Windows – gaban Mar 06 '12 at 11:48
0
echo mysql_query($query) or mysql_error()
sandeep
  • 2,244
  • 1
  • 23
  • 38
0

You aren't doing any error checking, and with the @ in front of commands even actively suppressing errors. Use this to check for errors with your query or database connection:

mysql_query($query) or die(mysql_error());

If you want to check your query but don't want it to delete everything, add another condition to it:

... AND 1=0

which is always false, thus nothing gets deleted. MySQL will still check your query and give you errors, if e.g. some fields or the table are unknown.

Hendrik Richter
  • 287
  • 2
  • 5