【问题标题】:Cant Fetch Data From MYSQL While Sending Email With PHP使用 PHP 发送电子邮件时无法从 MYSQL 获取数据
【发布时间】:2016-04-24 09:00:45
【问题描述】:

我正在向我的订阅者发送电子邮件,并且所有电子邮件地址都插入到 MYSQl 表中。但是有了它,我想发送一些位于同一数据库的其他表中的文章。请帮我。在“$name”中它只获取 1 行(我想获取那里给定 id 的所有数据)。

<?php if(isset($_POST['send'])){
$servername = "localhost";
$username = "username ";
$password = "password ";
$dbname = "db";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM table1 where id in (1, 3, 2, 4)";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {

$name = $row["title"];

}}else {}
$conn->close();
}
?>

<?php if(isset($_POST['send'])){
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "db";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT email, subid FROM table2";
$result = $conn->query($sql);

if ($result->num_rows > 0) {while($row = $result->fetch_assoc()){

$subid = $row["subid"];

$email_to = $row["email"];
$subject = "Newsletter | OnlineDealsIndia";
$header = "From: OnlineDealsIndia <noreply@onlinedealsindia.in>\r\n"; 
$header.= "MIME-Version: 1.0\r\n"; 
$header.= "Content-Type: text/html; charset=utf-8\r\n"; 
$header.= "X-Priority: 1\r\n"; 

$message = '<h3>Todays Top Deals!</h3><hr>

<li>' .$name. '</li>

<a href="http://www.onlinedealsindia.in/newsletter/unsubscribe/?subid=' .$subid. '">Click Here</a> To Unsubscribe
';
mail($email_to,$subject,$message,$header);
}}else {}

$conn->close();
}
?>

在“$name”中,它应该获取我在上面提供了 id 的所有行。

所有答案将不胜感激。谢谢!

【问题讨论】:

  • 在一个循环中,您获取$name,然后一遍又一遍地将其设置到同一个地方。您不想将其添加到数组中吗?
  • 不可能是“$name”应该获取所有 4 行并显示。
  • $name = $value 不会神奇地累积列表。 $name[] = $value 将添加到数组中。
  • 您可以将名称存储在一个数组中,也可以在while($row=$rs-&gt;fetch_assoc()) 循环中使用您的邮件函数
  • 您可以在编码中编辑并发送

标签: php mysql email


【解决方案1】:

这应该可以解决问题:

<?php if ( isset( $_POST['send'] ) ) {
    $servername = "localhost";
    $username   = "username ";
    $password   = "password ";
    $dbname     = "db";

    $conn = new mysqli( $servername, $username, $password, $dbname );
    if ( $conn->connect_error ) {
        die( "Connection failed: " . $conn->connect_error );
    }

    $sql    = "SELECT * FROM table1 where id in (1, 3, 2, 4)";
    $result = $conn->query( $sql );

    $subject = "Newsletter | OnlineDealsIndia";
    $header  = "From: OnlineDealsIndia <noreply@onlinedealsindia.in>\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: text/html; charset=utf-8\r\n";
    $header .= "X-Priority: 1\r\n";

    $message = '<h3>Todays Top Deals!</h3><hr><ul>';
    if ( $result->num_rows > 0 ) {
        while ( $row = $result->fetch_assoc() ) {
            $message .= '<li>' . $row["title"] . '</li>';
        }
    }
    $message .= '</ul><a href="http://www.onlinedealsindia.in/newsletter/unsubscribe/?subid=' . $subid . '">Click Here</a> To Unsubscribe';

    $sql    = "SELECT email, subid FROM table2";
    $result = $conn->query( $sql );

    if ( $result->num_rows > 0 ) {
        while ( $row = $result->fetch_assoc() ) {
            $subid    = $row["subid"];
            $email_to = $row["email"];
            mail( $email_to, $subject, $message, $header );
        }
    }

    $conn->close();
}
?>

【讨论】:

  • 打开关闭再重新打开似乎毫无意义。
  • 还有很多空间可以改进此代码,我做了尽可能少的更改以显示如何从问题中解决此特定问题。
  • 是的,不是真的责怪你,只是观察一下。
  • 那是什么原因呢?错误或未显示所有标题?
  • 好吧,我是 php 新手,所以如果你们请检查并给我正确的编码(它会比你更完整)
猜你喜欢
  • 1970-01-01
  • 2013-07-15
  • 2017-11-26
  • 2011-11-30
  • 2013-09-04
  • 2012-08-31
  • 2014-01-05
  • 2016-06-09
相关资源
最近更新 更多