Php 在mysql字段中值为1的电子邮件用户
我有一个php文件,每晚午夜执行。如果表中的所有用户在alerts字段中的值为“1”,它将查询mysql并返回他们的电子邮件地址。我正在尝试将此文件仅发送给那些用户。目前,我通过向数据库中的每个电子邮件地址发送电子邮件来实现这一功能,Php 在mysql字段中值为1的电子邮件用户,php,mysql,Php,Mysql,我有一个php文件,每晚午夜执行。如果表中的所有用户在alerts字段中的值为“1”,它将查询mysql并返回他们的电子邮件地址。我正在尝试将此文件仅发送给那些用户。目前,我通过向数据库中的每个电子邮件地址发送电子邮件来实现这一功能,$to=$row['email']但我无法让它只向已注册警报的选定用户发送电子邮件 <? $con=mysqli_connect("localhost","root","","DB1"); // Check connection if (mysqli_con
$to=$row['email']代码>但我无法让它只向已注册警报的选定用户发送电子邮件
<?
$con=mysqli_connect("localhost","root","","DB1");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM users");
##email users their account balances
$sql = mysqli_query("SELECT * FROM users WHERE alerts = '1'");
$recipients = array();
while($row = mysqli_fetch_array($result)){
$recipient[]=$row['email'];
$to = $row['email'];
$subject = "Your account has received a deposit.";
$body = "Hello,\n\nWe have added 1 DOGE and .0000001 BTC to your account. Go to www.bitcoinchew.com/Profile to withdrawl. $
if (mail($to, $subject, $body)) {
echo("<p>Email successfully sent!</p>");
} else {
echo("<p>Email delivery failed…</p>");
}
}
?>
您的while循环应为
while($row = mysqli_fetch_array($sql)){ ...
好的,给你。首先,您为查询调用了错误的变量$result
,并使用了mysqli\u query()
两次
$result = mysqli_query($con,"SELECT * FROM users");
及
摆脱$result=mysqli_查询($con,“SELECT*FROM users”)代码>
并使用$sql=mysqli\u查询($con,“从警报为'1'的用户中选择*)代码>
然后使用while($row=mysqli\u fetch\u数组($sql)){
您的中还缺少一个From:
,它很可能最终成为垃圾邮件或被“标记”为垃圾邮件。来自的将显示为您的服务器,而不是电子邮件地址
$from = "you@example.com";
然后在if(mail($to,$subject,$body))
中添加$headers,
if (mail($to, $subject, $body, $headers))
您在的末尾还有一个缺少的引号和分号要撤消。$
,这将导致分析错误
提取。$”;
这里有一个工作重写:
<?php
$con = mysqli_connect("localhost","root","","DB1");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = mysqli_query($con,"SELECT * FROM users WHERE alerts = '1'");
$recipients = array();
while($row = mysqli_fetch_array($sql)){
$recipient[]=$row['email'];
$from = "you@example.com"; // <= change it to your own
$to = $row['email'];
$subject = "Your account has received a deposit.";
$body = "Hello,\n\nWe have added 1 DOGE and .0000001 BTC to your account. Go to www.bitcoinchew.com/Profile to withdrawl. $";
$headers = "From: $from";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Email successfully sent!</p>");
} else {
echo("<p>Email delivery failed…</p>");
}
}
您的while($row=mysqli\u fetch\u数组($result)){
正在从$result=mysqli\u查询($con,“SELECT*from users”);
而不是$sql=mysqli\u查询(“SELECT*from users WHERE alerts='1');
编辑其中一个并删除另一个缺少$sql=mysqli\u查询($sql>从警报为“1”的用户中选择*;
应为$sql=mysqli\u查询($con,“从警报为“1”的用户中选择*);
<?php
$con = mysqli_connect("localhost","root","","DB1");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = mysqli_query($con,"SELECT * FROM users WHERE alerts = '1'");
$recipients = array();
while($row = mysqli_fetch_array($sql)){
$recipient[]=$row['email'];
$from = "you@example.com"; // <= change it to your own
$to = $row['email'];
$subject = "Your account has received a deposit.";
$body = "Hello,\n\nWe have added 1 DOGE and .0000001 BTC to your account. Go to www.bitcoinchew.com/Profile to withdrawl. $";
$headers = "From: $from";
if (mail($to, $subject, $body, $headers)) {
echo("<p>Email successfully sent!</p>");
} else {
echo("<p>Email delivery failed…</p>");
}
}