接收用户输入以使用键盘上的用户输入值运行 php 脚本

发布于 2024-11-03 17:33:15 字数 288 浏览 5 评论 0 原文

我有一个 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦忆晨望 2024-11-10 17:33:15

您可以通过 $argc(cli 参数计数)和 $argv(实际参数值数组)检查命令行参数。或者,您可以提示输入并通过读取标准输入 (STDIN) 来获取该输入。

给定一个命令行:

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

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.
唯憾梦倾城 2024-11-10 17:33:15

一行代码(第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;

http://oneqonea.blogspot.com/2012/04/how-can-i-capture-user-input-from-cmd.html

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

煮酒 2024-11-10 17:33:15

“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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文