Jump to content

PHP Script Review - need solution!


jjfletcher90

Recommended Posts

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

 

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

mysql_select_db("genpublic", $db);

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

mysql_close($db);
?>

 

Best Regards,

 

John

 

MOD EDIT: DB credentials edited out

Link to comment
Share on other sites

I recieve the following errors:

 

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

 

 

Link to comment
Share on other sites

This is all in testing stages - trying to get the code to delete a record in the database after a set amount of time has passed:

 

the code I am testing is:

 

<?php
$db = mysql_connect("credentials removed");
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);
?>

Link to comment
Share on other sites

Well, that explains a lot; I wish you'd said that to begin with. You only enter the actual query string itself in phpMyAdmin, not the call to mysql_query(). Try it that way, and see what happens.

 

In other words, in phpMyAdmin, only paste this

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

Link to comment
Share on other sites

Error

There seems to be an error in your SQL query. The MySQL server error output below, if there is any, may also help you in diagnosing the problem

 

ERROR: Unclosed quote @ 75

STR: "

SQL: DELETE FROM users

WHERE `signup_date` <DATE_SUB(CURDATE(), INTERVAL 1 DAY)");

 

 

SQL 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 '")' at line 2

 

 

Link to comment
Share on other sites

removered the " enclosed and re-ran the test:

 

Error

SQL 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 ')' at line 1

 

Link to comment
Share on other sites

Deleted rows: 0 (Query took 0.0004 sec)

 

SQL query:

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

 

checked to see if the sample data was deleted from the table and it is still there - huh?

 

Looks like progress to me - at least no errors!

 

No to actually deleted the record when requested...

Link to comment
Share on other sites

If you're trying to delete any record that has a date in that field of more than 1 day ago, yes. Note that the date would need to be more than one day ago, so it won't delete a record with yesterday's date. Just to make sure, the signup_date field is a DATE type field, right?

Link to comment
Share on other sites

I ran this from within phpmyadmin:

 

and the record deleted... I went in and edited the date to reflect 2 days ago and then it deleted... So.... 

 

When I run the PHP from the browser this is the error recieved:

 

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

 

This is the modified code:

<?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);
?>

 

Thanks for helping me Guys - your awsome!

 

Link to comment
Share on other sites

Your call to mysql_query() isn't properly closed. It's really better practice not to embed the query string within the query execution, as doing so makes it impossible to echo the query string if you need to do any debugging.

 

mysql_select_db("databasename", $db);
$query = "DELETE FROM users WHERE `signup_date` <DATE_SUB(CURDATE(), INTERVAL 1 DAY)";
mysql_query( $query );
// etc.

Link to comment
Share on other sites

I'm just getting darn right angry now... this is the new error after all the changes:

 

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

 

<?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);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.