0

I have a PHP script that is called from PayPal IPN. When a user purchases something form my site, the IPN PHP script creates the user in the mysql database. At a later time, 24hrs, that user needs to be deleted from the database. I assume that using the "at" command would work. I believe the problem I am running into is regarding the parentheses in my command.

<?php
$email = "[email protected]";
$cmd = 'mysql -u username -pPassword -e `DELETE FROM db.users WHERE users.email = "$email";`|at tomorrow';
exec($cmd);
?>

If I run this page as-is, it throws the following error:

warning: commands will be executed using /bin/sh
sh: 1: DELETE: not found
mysql: option '-e' requires an argument
job 38 at Sun Feb  8 04:59:00 2015

That is what is leading me to believe there is a problem with my quotes and parentheses.

  • Why not use the mysqli function set? That's what they're there for – Machavity Feb 8 '15 at 3:51
  • Because I need it to be scheduled to run at a later date. Unless you know of a method within the mysql function set that will accomplish that.. – user2341830 Feb 8 '15 at 3:59
  • That's what cron jobs are for. Runs at a set time but runs a pure PHP script to access your database – Machavity Feb 8 '15 at 4:14
0

You should explain the problem you're running into. The first thing I note is that you use single quotes - those don't interpret the inner string so $email doesn't get replaced for the e-mail value.

Second - you're using backticks. The command gets executed in a bash shell, where the backticks around your query get interpreted as a shell command. Try this: $cmd = "echo mysql -u username -pPassword -e \\'DELETE FROM db.users WHERE users.email = \\\"$email\\\"\\' |at tomorrow";

  • As for using at to schedule, I'm not sure that's going to work; it depends on server configuration (especially if you're running on a system you don't administer yourself). Generally such tasks would be solved by adding the tasks to a queue and scheduling a script run (using crontab, for example) which would execute the necessary commands from the queue. – AdrianH Feb 8 '15 at 3:57
  • It is my personal server. I have used AT many times in the past and it has worked great for me. I am open to cron jobs if I have a thought... The problem still is within the $cmd string. I have my first set of parentheses around the entire command, then another set around the SQL query, then a third around the $email variable. That doesnt seem to work well.. – user2341830 Feb 8 '15 at 4:02
  • That did it! Thanks for the explanation! – user2341830 Feb 8 '15 at 4:08
  • You wouldnt happen to know why the sql query is executed immediately and not being passed to the AT command? I check the database, and the user is deleted immediately. I check the AT queue, and there is nothing in it. – user2341830 Feb 8 '15 at 4:27
  • You're right, I forgot about that. It's going to execute the first part of the command, generate its output and try to pass that to "at". Try echoing the first part - $cmd = "echo mysql ... " – AdrianH Feb 8 '15 at 4:39
0

try this :

"mysql -u username -pPassword -e `DELETE FROM db.users WHERE users.email = '$email'`|at tomorrow";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.