作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好吧,长话短说,我有一个简单的 mailto 函数,我想为数据库列表上的每个名称应用/运行。由于它不起作用,我从中删除了所有邮件内容并进行测试以确保 while 循环与数据库一起工作,这样做了
<?php
$connect2db = mysqli_connect('127.0.0.1','root','pass','dbnamehere');
if(!$connect2db){
die("Sorry but theres a connection to database error" . mysqli_error);
}
$sn_query = "SELECT * FROM email_list";
$sn_queryResult = mysqli_query($connect2db, $sn_query) or die("Sorry but theres a connection to database error" . mysqli_error);
$sn_rowSelect = mysqli_fetch_array($sn_queryResult);
$to = $sn_rowSelect;
?>
<br/><br/>
////lower part on page //////<br/><br/>
<?php
while($sn_rowSelect = mysqli_fetch_array($sn_queryResult) ) {
echo "hello there" . " " . $sn_rowSelect['firstname'] . " <br/>";
}
?>
现在可以了,它会遍历我的数据库并从数据库列表中打印出我的所有名字。在我的菜鸟大脑中,我认为如果我删除回显线,并输入适当的 mailto 信息,它会像以前一样循环,但将邮件发送到每个名称。所以我这样做了:
<?php
$sn_query = "SELECT email FROM email_list";
$sn_queryResult = mysqli_query($connect2db, $sn_query) or die("Sorry but theres a connection to database error" . mysqli_error);
$sn_rowSelect = mysqli_fetch_array($sn_queryResult);
$to = implode(",",$sn_rowSelect);
$from = $_POST['sender'];
$subject = $_POST['subj'];
$mssg = $_POST['message'];
$headers = "MIME-Version: 1.0rn";
$headers .= "From: $from\r\n";
$mailstatus = mail($to, $subject, $mssg, $headers);
?>
<br/><br/>
//////////<br/><br/>
<?php
while($sn_rowSelect = mysqli_fetch_array($sn_queryResult) ) {
$mailstatus;
if($mailstatus) {
echo "Success";
}else{
echo "There was a problem sending the mail. Check your code and make sure that the e-mail address $to is valid\n";
}
}
?>
现在,这会通过电子邮件发送我列表中的第一个名字,但不会发送其余的名字。我没有收到任何错误,所以不确定问题是什么。我本来打算尝试使用 num_rows
执行 if 语句,但在 StackOverflow 上的其他地方,有人说这没有帮助,因为 while 循环自己处理了它。 (无论哪种方式我都尝试过,但它仍然只通过电子邮件发送名字)我在这里尝试但无济于事。
最佳答案
您尚未在循环内调用 mail()
函数。你在外面叫一次。相反,请尝试以下操作。
假设您已从数据库查询中检索到 $to
地址(就像在测试中对名字所做的那样),从行集中提取它,并在 mail()
:
while($sn_rowSelect = mysqli_fetch_array($sn_queryResult) ) {
// Get the $to address:
$to = $sn_rowSelect['email'];
// Call mail() inside the loop.
$mailstatus = mail($to, $subject, $mssg, $headers);
if($mailstatus) {
echo "Success";
}else{
echo "There was a problem sending the mail. Check your code and make sure that the e-mail address $to is valid\n";
}
}
另请注意,由于您在脚本顶部调用 mysql_fetch_array()
,因此 while 循环将从第二行开始。您应该删除循环之前发生的对 mysql_fetch_array()
的第一次调用。
$sn_queryResult = mysqli_query($connect2db, $sn_query) or die("Sorry but theres a connection to database error" . mysqli_error);
// Don't want this...
//$sn_rowSelect = mysqli_fetch_array($sn_queryResult);
关于php - While 循环遍历 mysql 数据库列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7068159/
我是一名优秀的程序员,十分优秀!