2

I have a php script, which I run from the command line in linux (NOT interactive), which does a lookup in a mysql database and based on the value of field "SentOrNotSent" either sends emails or not. It works fine, this php script.

Now I want to modify the script so that when it first runs, it asks for an "ID" number for input from the keyboard, then it goes into the MySql database and deletes the record with that ID. I know how to do the delete, I'm lost on how to get this script to ask for input, then use that value for the part of the mysql update statement to delete the record.

This script is ONLY run from the command line and is based in a directory that ONLY the administrator has access to.

thank you for any help.

0

3 Answers 3

6

You can either check for command line arguments, via $argc (count of cli args) and $argv (array of actual argument values). Or you can prompt for input and get that input by reading from standard input (STDIN).

Given a command line of:

$ ./myscript 1234

myscript.php:
<?php
if (isset($argv[1])) {
   $myid = (int)$argv[1]; // use the command line argument for ID
} else {
   print("Enter an ID number: ");
   $myid = (int)fgets(STDIN); // prompt the user for an ID
}
... proceed with $myid = 1234 or whatever you entered.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That's does it great!
3

One line of code (line 2):

<?php
$id = trim(shell_exec("read -p 'Enter your ID: ' id\necho \$id"));
echo "The ID you entered was $id (this is PHP speaking… do your database stuff next…)\n";
exit;

Checkout this answer's source at http://oneqonea.blogspot.com/2012/04/how-can-i-capture-user-input-from-cmd.html

Comments

0

"php script.php 1234" just add the id, does it really need to be interactive? 1234 will be in the $argv array, you can then use in the script

http://php.net/manual/en/reserved.variables.argv.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.