通过Cron Job和PHP MySQL查询实施生日提醒

发布于 2025-01-29 19:55:38 字数 1350 浏览 2 评论 0原文

我已经阅读了有关Cron作业的其他问题,这些问题通过PHP查询运行,但是我遇到的错误似乎是特定于语法的,我希望有人能提供帮助。 (我完全是新手,并且自学,所以愚蠢的错误潜力很高)。

我正在尝试建立每天运行一次的Cron工作。它的工作是查询居住在同一家服务器上的MySQL数据库中的用户列表,确定当天的任何一个生日是否有生日,并回应了Cron工作的响应,以通过电子邮件发送给我。

这是数据库创建代码:

CREATE TABLE IF NOT EXISTS test ( sid INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, birthday DATE );
insert into test values(NULL,'Jane Doe','2022-5-17');
insert into test values(NULL,'John Doe','2022-5-20');

这是我的php文件(生日checker.php):

<?php 
$conn = new mysqli('server', 'username', 'password', 'schema');
// Check connection
if ($conn->connect_error) {
  die('Connection failed: ' . $conn->connect_error);
}
$result = $conn->query('select Name from test where MONTH(Birthday)=MONTH(CURRENT_DATE) and DAYOFMONTH(Birthday)=DAYOFMONTH(CURRENT_DATE)');
foreach ($result as $birthday)
{
    echo 'Heads up: ' . $birthday['Name'] . ' has a birthday today!';
}

我不确定如何共享cron设置,但是现在每十分钟运行一次。我知道它正在运行,因为我收到了电子邮件,但这就是他们所说的:

fullpath/birthdayChecker.php: line 1: ?php: No such file or directory
fullpath/birthdayChecker.php: line 2: syntax error near unexpected token `('
fullpath/birthdayChecker.php: line 2: `$conn = new mysqli('server', 'username', 'password', 'schema');'

特别是考虑到第1行错误,我敢肯定我只是在做一些愚蠢的事情,但我看不到。感谢任何帮助!

I've read through other questions regarding cron jobs which run through php queries, but the error I'm getting seems syntax-specific and I'm hoping someone can help. (I'm new at all this and teaching myself, so dumb mistake potential is high).

I'm trying to set up a cron job which runs once per day. Its job is to query a list of users in a mysql database that lives on the same server, determine if any of them have a birthday on that day and echo the response for the cron job to email to me.

Here is the database creation code:

CREATE TABLE IF NOT EXISTS test ( sid INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, birthday DATE );
insert into test values(NULL,'Jane Doe','2022-5-17');
insert into test values(NULL,'John Doe','2022-5-20');

Here's my php file (birthdayChecker.php):

<?php 
$conn = new mysqli('server', 'username', 'password', 'schema');
// Check connection
if ($conn->connect_error) {
  die('Connection failed: ' . $conn->connect_error);
}
$result = $conn->query('select Name from test where MONTH(Birthday)=MONTH(CURRENT_DATE) and DAYOFMONTH(Birthday)=DAYOFMONTH(CURRENT_DATE)');
foreach ($result as $birthday)
{
    echo 'Heads up: ' . $birthday['Name'] . ' has a birthday today!';
}

I'm not totally sure how to share the cron setup, but it's setup to run once every ten minutes right now. I know it's running, because I'm getting the emails, but this is what they say:

fullpath/birthdayChecker.php: line 1: ?php: No such file or directory
fullpath/birthdayChecker.php: line 2: syntax error near unexpected token `('
fullpath/birthdayChecker.php: line 2: `$conn = new mysqli('server', 'username', 'password', 'schema');'

especially given that line 1 error, I'm pretty sure I'm just doing something stupid, but I can't see it. Appreciate any help!

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

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

发布评论

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

评论(1

再见回来 2025-02-05 19:55:38

事实证明,我需要告诉Cron的工作使用PHP(Shell知道,但显然没有Cron):

php -f fullpath/birthdayChecker.php

这也需要更多地利用实际的PHP代码才能使其合作:

$query = "select Name from test where MONTH(Birthday)=MONTH(CURRENT_DATE) and DAYOFMONTH(Birthday)=DAYOFMONTH(CURRENT_DATE)";
$result = $conn->query($query)->fetch_all(MYSQLI_ASSOC);

foreach($result as $row)
{
  echo "Heads up: " . $row["Name"] . " has a birthday today!";
}

The problem turned out to be that I needed to tell the cron job to use php (the shell knows, but cron doesn't apparently):

php -f fullpath/birthdayChecker.php

it also took a little more fiddling with the actual php code to get it to cooperate:

$query = "select Name from test where MONTH(Birthday)=MONTH(CURRENT_DATE) and DAYOFMONTH(Birthday)=DAYOFMONTH(CURRENT_DATE)";
$result = $conn->query($query)->fetch_all(MYSQLI_ASSOC);

foreach($result as $row)
{
  echo "Heads up: " . $row["Name"] . " has a birthday today!";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文