2016-04-24 56 views
0

我正在向我的訂閱者發送電子郵件,並且所有電子郵件地址都被插入到MYSQl的表中。但有了它,我想發送一些位於同一數據庫的其他表中的文章。請幫幫我。在「$ name」中它只提取1行(我想獲取所有給定ID的數據)。無法使用PHP發送電子郵件時從MYSQL獲取數據

<?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 <[email protected]>\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的所有行。

所有的答案將不勝感激。謝謝!

+0

在一個循環中,您要提取'$ name',然後一遍一遍將它設置爲同一個地方。你不想把它添加到數組嗎? – tadman

+0

它不能是「$ name」應該獲取所有4行並顯示。 –

+0

'$ name = $ value'不會神奇地累積列表。 '$ name [] = $ value'將被添加到數組中。 – tadman

回答

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 <[email protected]>\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(); 
} 
?> 
+0

打開和關閉以及然後重新開放似乎毫無意義。 – tadman

+0

有很多空間來改善這個代碼,我儘可能少地做了一些修改,使它能夠顯示如何解決這個問題的特定問題。 –

+0

是的,不是真的讓你失望,只是做一個觀察。 – tadman