Page 1 of 2

PHP Script Review

Posted: Fri Aug 05, 2011 11:36 am
by jjfletcher90
I have a database in MySql and runs fine - yet the server I am on is a Windows running MySql 5.0 and not 5.1.6 - unable to run cron job with this version and also windows i understand does not support.... So I have created a PHP script that I hope will provide the same results - but when I run it, the data in my database does not delete as intended... Could you folks review my script and possibly any othe thoughts on how to achieve this function...

Code: Select all

<?php
$db = mysql_connect("removed credentials");
if (!$db)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("databasename", $db);

mysql_query("DELETE FROM users
WHERE `signup_date` <DATE_SUB(CURDATE(), INTERVAL 1 DAY)'");

mysql_close($db);
?>

Best Regards,

John

Re: PHP Script Review

Posted: Fri Aug 05, 2011 11:45 am
by Dodon
What is the result if you place the query directly into phpmyadmin? Do you get an error?

Are there records in your table that meet those criteria?

try

Code: Select all

$result = mysql_query("DELETE FROM users WHERE signup_date < DATE_SUB(CURDATE(), INTERVAL 1 DAY)");
if (!$result) {
    die('Invalid query: ' . mysql_error());
}
 
to see if you get any errors.

Re: PHP Script Review

Posted: Fri Aug 05, 2011 11:47 am
by Dodon
Ooh wait,

Code: Select all

mysql_query("DELETE FROM users
 WHERE `signup_date` <DATE_SUB(CURDATE(), INTERVAL 1 DAY)'");
 
there is an extra ' after DAY)

Re: PHP Script Review

Posted: Fri Aug 05, 2011 11:51 am
by jjfletcher90
SQL query:

mysql_query(

"DELETE FROM users
WHERE `signup_date` <DATE_SUB(CURDATE(), INTERVAL 1 DAY)'"
)

MySQL said:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql_query("DELETE FROM users
WHERE `signup_date` <DATE_SUB(CURDATE(), INTERV' at line 1

Re: PHP Script Review

Posted: Fri Aug 05, 2011 11:54 am
by Dodon
Like I said you got an extra ' in your query after DAY) remove it and see if it works

Re: PHP Script Review

Posted: Fri Aug 05, 2011 11:59 am
by jjfletcher90
removed the extra ' and ran the test and received this:

Error
SQL query:

mysql_query(

"DELETE FROM users
WHERE `signup_date` <DATE_SUB(CURDATE(), INTERVAL 1 DAY)"
)

MySQL said:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql_query("DELETE FROM users
WHERE `signup_date` <DATE_SUB(CURDATE(), INTERV' at line 1

Re: PHP Script Review

Posted: Fri Aug 05, 2011 12:01 pm
by jjfletcher90
changed to :

mysql_query("DELETE FROM users
WHERE `signup_date` <DATE_SUB(CURDATE(), INTERVAL 1 DAY)");

Re: PHP Script Review

Posted: Fri Aug 05, 2011 1:39 pm
by jjfletcher90
successful in deleting record from within the phpmyadmin

DELETE FROM users WHERE `signup_date` <DATE_SUB(CURDATE(), INTERVAL 1 DAY)

yet received this error when the php script is run from the browser:

Parse error: syntax error, unexpected $end in test.php on line 13

Re: PHP Script Review

Posted: Fri Aug 05, 2011 2:02 pm
by Celauran
jjfletcher90 wrote:successful in deleting record from within the phpmyadmin

DELETE FROM users WHERE `signup_date` <DATE_SUB(CURDATE(), INTERVAL 1 DAY)

yet received this error when the php script is run from the browser:

Parse error: syntax error, unexpected $end in test.php on line 13
Please post the relevant code.

Re: PHP Script Review

Posted: Fri Aug 05, 2011 2:06 pm
by jjfletcher90
<?php
$db = mysql_connect("removed credentials");
if (!$db)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("databasename", $db);

mysql_query("DELETE FROM users WHERE `signup_date` <DATE_SUB(CURDATE(), INTERVAL 1 DAY)

mysql_close($db);
?>

Re: PHP Script Review

Posted: Fri Aug 05, 2011 2:10 pm
by Celauran
You're missing a closing quote in and a semi-colon after your query.

Code: Select all

mysql_query("DELETE FROM users WHERE `signup_date` <DATE_SUB(CURDATE(), INTERVAL 1 DAY)");

Re: PHP Script Review

Posted: Fri Aug 05, 2011 2:13 pm
by jjfletcher90
added the semi-colon after the query........

Still receive the following error when run from the browser:

Parse error: syntax error, unexpected $end in test.php on line 13

Re: PHP Script Review

Posted: Fri Aug 05, 2011 2:14 pm
by Dodon
mysql_query("DELETE FROM users WHERE `signup_date` <DATE_SUB(CURDATE(), INTERVAL 1 DAY)

has to be

mysql_query("DELETE FROM users WHERE `signup_date` <DATE_SUB(CURDATE(), INTERVAL 1 DAY)");

Re: PHP Script Review

Posted: Fri Aug 05, 2011 2:14 pm
by Celauran
Did you copy-paste my code above? You had a missing closing quote, closing parenthesis, and semi-colon.

Re: PHP Script Review

Posted: Fri Aug 05, 2011 2:20 pm
by jjfletcher90
OK sorry - disregard the previous post reply - forgot the "); - Finally - this works and deletes the record from the database - outstanding... now the next problem is how do I get the PHP to run by itself - the Windows server I am on does not feature Cronjob?