从电子邮件中的数据库中检索数据

时间:2013-09-09 05:42:49

标签: php email

我使用sendmail在php中发送电子邮件。问题是,我想在邮件中检索数据库中的详细信息。我怎样才能做到这一点 ?这是我的代码。

<?php
// set database server access variables: 
$host = "localhost"; 
$user = "root"; 
$pass = ""; 
$db = "master_inventory";

// open connection 
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 

// select database 
mysql_select_db($db) or die ("Unable to select database!"); 

// create query 
$query = "SELECT * FROM desktop"; 

// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

// see if any rows were returned 
if (mysql_num_rows($result) > 0) { 

$to       = 'nazasdei@domain.com';
$subject  = 'warranty licensed expired';
$message  = 'The Following Your Product Expired
          Product Code:.$row[Desk_PC_Name]';
$headers  =  'From: miloe@gmail.com' . "\r\n" .
             'Reply-To: miloe@gmail.com' . "\r\n" .
             'MIME-Version: 1.0' . "\r\n" .
             'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
             'X-Mailer: PHP/' . phpversion();
}       
if(mail($to, $subject, $message, $headers))
echo "Email sent";
else
echo "Email sending failed";

?>

2 个答案:

答案 0 :(得分:0)

// execute query 
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

// see if any rows were returned 
if (mysql_num_rows($result) > 0) { 
while($row = mysql_fetch_array($result)){
$code = $row['Desk_PC_Name'];

}

$to       = "nazasdei@domain.com";
$subject  = "warranty licensed expired";
$message  = "The Following Your Product Expired
          Product Code:.$code";

$headers  =  'From: miloe@gmail.com' . "\r\n" .
             'Reply-To: miloe@gmail.com' . "\r\n" .
             'MIME-Version: 1.0' . "\r\n" .
             'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
             'X-Mailer: PHP/' . phpversion();       
if(mail($to, $subject, $message, $headers))
echo "Email sent";
else
echo "Email sending failed";

但是这些mysql_*函数已弃用PDO

答案 1 :(得分:0)

试试这个

$query = "SELECT * FROM desktop";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
while($row = mysql_fetch_array($result)){
   $Desk_PC_Name = $row['Desk_PC_Name'];
}
$to       = "nazasdei@domain.com"; // You can use email from database like this $row['email'];
$subject  = "warranty licensed expired";
$message  = "The Following Your Product Expired
          Product Code:.$Desk_PC_Name";

$headers  =  'From: miloe@gmail.com' . "\r\n" .
             'Reply-To: miloe@gmail.com' . "\r\n" .
             'MIME-Version: 1.0' . "\r\n" .
             'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
             'X-Mailer: PHP/' . phpversion();       
if(@mail($to, $subject, $message, $headers)) {
   echo "Email sent";
} else {
   echo "Email sending failed";
}

使用@符号忽略错误