我有一个 php 脚本,我从 Linux 中的命令行运行(非交互式),它在 mysql 数据库中进行查找,并根据字段“SentOrNotSent”的值发送或不发送电子邮件。这个 php 脚本工作正常。
现在我想修改脚本,以便当它第一次运行时,它要求从键盘输入一个“ID”号,然后它进入MySql数据库并删除具有该ID的记录。我知道如何进行删除,但我不知道如何让这个脚本请求输入,然后使用该值作为 mysql 更新语句的一部分来删除记录。
该脚本只能从命令行运行,并且位于只有管理员有权访问的目录中。
感谢您的帮助。
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.
发布评论
评论(3)
您可以通过 $argc(cli 参数计数)和 $argv(实际参数值数组)检查命令行参数。或者,您可以提示输入并通过读取标准输入 (STDIN) 来获取该输入。
给定一个命令行:
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:
一行代码(第2行):
在 http://oneqonea.blogspot.com/2012/04/how-can-i-capture-user-input-from-cmd.html
One line of code (line 2):
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”只是添加id,它真的需要交互吗?
1234 将位于 $argv 数组中,然后您可以在脚本中使用
http:// /php.net/manual/en/reserved.variables.argv.php
"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