使用PHP发送电子邮件时无法从MYSQL获取数据

时间:2016-04-24 09:00:46

标签: php mysql email

我正在向我的订阅者发送电子邮件,所有电子邮件地址都插入到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();
}
?>

In&#34; $ name&#34;它应该获取我在上面提供id的所有行。

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

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题:

<?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();
}
?>