PHP Script Review

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

jjfletcher90
Forum Newbie
Posts: 18
Joined: Sat Jul 09, 2011 12:38 pm

PHP Script Review

Post 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
Last edited by jjfletcher90 on Fri Aug 05, 2011 2:08 pm, edited 1 time in total.
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

Re: PHP Script Review

Post 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.
Last edited by Dodon on Fri Aug 05, 2011 12:09 pm, edited 1 time in total.
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

Re: PHP Script Review

Post 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)
jjfletcher90
Forum Newbie
Posts: 18
Joined: Sat Jul 09, 2011 12:38 pm

Re: PHP Script Review

Post 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
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

Re: PHP Script Review

Post by Dodon »

Like I said you got an extra ' in your query after DAY) remove it and see if it works
jjfletcher90
Forum Newbie
Posts: 18
Joined: Sat Jul 09, 2011 12:38 pm

Re: PHP Script Review

Post 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
jjfletcher90
Forum Newbie
Posts: 18
Joined: Sat Jul 09, 2011 12:38 pm

Re: PHP Script Review

Post by jjfletcher90 »

changed to :

mysql_query("DELETE FROM users
WHERE `signup_date` <DATE_SUB(CURDATE(), INTERVAL 1 DAY)");
jjfletcher90
Forum Newbie
Posts: 18
Joined: Sat Jul 09, 2011 12:38 pm

Re: PHP Script Review

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP Script Review

Post 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.
jjfletcher90
Forum Newbie
Posts: 18
Joined: Sat Jul 09, 2011 12:38 pm

Re: PHP Script Review

Post 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);
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP Script Review

Post 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)");
jjfletcher90
Forum Newbie
Posts: 18
Joined: Sat Jul 09, 2011 12:38 pm

Re: PHP Script Review

Post 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
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

Re: PHP Script Review

Post 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)");
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP Script Review

Post by Celauran »

Did you copy-paste my code above? You had a missing closing quote, closing parenthesis, and semi-colon.
jjfletcher90
Forum Newbie
Posts: 18
Joined: Sat Jul 09, 2011 12:38 pm

Re: PHP Script Review

Post 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?
Post Reply