Jump to content

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
https://forums.phpfreaks.com/topic/243960-php-script-review-need-solution/
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

 

 

Judging from the error message, it appears you've nested 2 mysql_query() functions somehow, like mysql_query( mysql_query("SELECT . . . ) ). Is that a possibility? Is the code you posted the actual code you're using?

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

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)

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

 

 

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

 

No, it executes fine for me locally. Copy/paste the actual query string into phpMyAdmin, don't retype it. See what happens.

 

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

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

These are the columns in the table that I am using - am I calling on the right column to delete the requested record based on lapsed time?

 

userid | first_name | last_name | email_address | username | password | info | user_level | signup_date | last_login | activated

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?

You're going to need to post some of the data from that particular field so we can see what's going on with it. Post the values this query returns.

 

SELECT signup_date FROM users ORDER BY signup_date DESC LIMIT 10

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!

 

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.

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

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.