当单元格值等于或小于PHP / mySQL中的数字时,发送电子邮件警报。可能?

时间:2011-10-24 17:57:45

标签: mysql

我有一个mySQl。我有一个库存数据库。我想在库存等于或小于预设值时向某人发送电子邮件。

这可能在客户端或服务器端吗?用PHP?

任何例子?

埃里克

3 个答案:

答案 0 :(得分:0)

如果您的服务器已设置好,则可以使用mail function

答案 1 :(得分:0)

PHP在服务器端运行,这回答了你的第一个问题。然后你需要知道你的托管服务器是否可以使用php邮件功能,或者你需要使用一些像swiftmailer这样的php库

我不知道你的数据库结构是什么样的,只是选择所有用户的所有电子邮件字段,其中invertvert等于或小于预设值。

最后遍历每个结果并使用我上面提到的函数发送邮件。

我建议在你的问题中添加数据库信息,因为很难以这种方式提供帮助。

答案 2 :(得分:0)

要通过cron(或计划任务)进行测试,请设置这样的脚本每隔几分钟运行一次。如果您想要聪明,请将通知阈值与产品一起存储在数据库中。这样您就可以在循环中通知多个人多个产品。

#!/usr/bin/php -q
<?php
// Your preset inventory...
$threshold = 5;
$recipient = "email@example.com";

$result = mysql_query("SELECT inventory FROM tbl WHERE product='whatever';");
if ($result) {
  $row = mysql_fetch_assoc($result);
  $inventory = $row['inventory'];
  if ($inventory <= $threshold) {
    $msg = "Inventory for product has fallen beneath threshold. $inventory remaining.";
    mail($recipient, "Inventory check below threshold", $msg);
  }
}
else {
  $msg = "An error occurred while checking inventory: " . mysql_error();
  mail($recipient, "Inventory check error", $msg);
}
?>

通过cron每隔十分钟运行一次:

 */10 * * * * /path/to/this/script.php