开发者

Recieve user input to run php script with that user input value from keyboard

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 an开发者_开发百科d is based in a directory that ONLY the administrator has access to.

thank you for any help.


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.


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


"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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜