2012-01-10 126 views
0

我想使用的PHPMailer发出简讯,但每次我试图触发它的时候我不断收到以下错误。我不确定我的sql语法是否正确?PHPMailer的投掷MySQL错误

Warning: mysql_fetch_array() : supplied argument is not a valid MySQL result resource in view.html.php(38):eval()'d code on line 32

<?php 
$formid = $_GET[token]; 
$templatequery = mysql_query(" 
    SELECT * 
    FROM hqfjt_chronoforms_data_addmailinglistmessage 
    WHERE cf_id = '$formid'" 
) or die(mysql_error()); 

$templateData = mysql_fetch_object($templatequery); 

$gasoiluserTemplate = $templateData->gasoilusers; 
$dervuserTemplate = $templateData->dervusers; 
$kerouserTemplate = $templateData->kerousers; 
$templateMessage = $templateData->mailinglistgroupmessage; 
?> 

<?php 
require_once('./send/class.phpmailer.php'); 
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded 
$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch 

// $body = file_get_contents('contents.html'); 
$body = 'Dear Test this is a test.'; 
// $body = preg_replace('/\\\\/i', $body); 

$mail->SetFrom('[email protected]', 'List manager'); 
$mail->AddReplyTo('[email protected]', 'List manager'); 

$mail->Subject = "Mailing List Test"; 

$query = " 
    SELECT leadname,businessname,email 
FROM hqfjt_chronoforms_data_addupdatelead 
WHERE keromailinglist='$kerolist' 
    AND dervmailinglist='$dervlist' 
    AND gasoilmailinglist='$gasoillist'"; 
$result = @MYSQL_QUERY($query); 

while ($row = mysql_fetch_array ($result)) { 
    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test 
    $mail->MsgHTML($body); 
    $mail->AddAddress($row["email"], $row["full_name"]); 
    $mail->AddStringAttachment($row["photo"], "YourPhoto.jpg"); 

    if(!$mail->Send()) { 
     echo "Mailer Error (" . str_replace("@", "&#64;", $row["email"]) . ') ' . $mail->ErrorInfo . '<br>'; 
    } else { 
     echo "Message sent to :" . $row["full_name"] . ' (' . str_replace("@", "&#64;", $row["email"]) . ')<br>'; 
    } 
    // Clear all addresses and attachments for next loop 
    $mail->ClearAddresses(); 
    $mail->ClearAttachments(); 
} 
?> 

编辑>>>>>>

我现在已经添加了错误检查,但现在只是得到了一个空白页,没有错误,也没有邮件?

  <?php 

        $formid = $_GET[token]; 
      $templatequery = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addmailinglistmessage WHERE cf_id = '$formid'") or die(mysql_error()); 
      $templateData = mysql_fetch_object($templatequery); 

      $gasoiluserTemplate = $templateData->gasoilusers; 
      $dervuserTemplate = $templateData->dervusers; 
      $kerouserTemplate = $templateData->kerousers; 
      $templateMessage = $templateData->mailinglistgroupmessage; 
       ?> 
        <?php 
      require_once('./send/class.phpmailer.php'); 
      //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded 

      $mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch 

      // $body    = file_get_contents('contents.html'); 

      $body = 'Dear Test this is a test.'; 

      // $body = preg_replace('/\\\\/i', $body); 

      $mail->SetFrom('[email protected]', 'List manager'); 
      $mail->AddReplyTo('[email protected]', 'List manager'); 

      $mail->Subject  = "Mailing List Test"; 

      $query = "SELECT leadname,businessname,email FROM hqfjt_chronoforms_data_addupdatelead WHERE keromailinglist='$kerolist' AND dervmailinglist='$dervlist' AND gasoilmailinglist='$gasoillist'"; 
      $result = mysql_query($query); 

      // Bail out on error 
if (!$result) 
    { 
    trigger_error("Database error: ".mysql_error()." Query used was:  ".htmlentities($query), E_USER_ERROR); 
    die(); 
    } 


      while ($row = mysql_fetch_array ($result)) { 
       $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test 
       $mail->MsgHTML($body); 
       $mail->AddAddress($row["email"], $row["full_name"]); 
       $mail->AddStringAttachment($row["photo"], "YourPhoto.jpg"); 

       if(!$mail->Send()) { 
       echo "Mailer Error (" . str_replace("@", "&#64;", $row["email"]) . ') ' . $mail->ErrorInfo . '<br>'; 
       } else { 
       echo "Message sent to :" . $row["full_name"] . ' (' . str_replace("@", "&#64;", $row["email"]) . ')<br>'; 
       } 
       // Clear all addresses and attachments for next loop 
       $mail->ClearAddresses(); 
       $mail->ClearAttachments(); 
      } 
      ?> 
+0

为什么您使用@MYSQL_QUERY – 2012-01-10 20:01:54

+1

尝试在你的MYSQL_QUERY($查询)行的前面去掉@符号,看看它在做什么。你很可能会压制一个错误信息,告诉你更详细的问题。 – Tanoro 2012-01-10 20:02:57

回答

3

你的代码,它没有做任何错误检查,所以这是毫不奇怪的查询打破静默,当它失败。检查错误,它会告诉你什么是错误的 - 如何做到这一点在manual on mysql_query()或这reference question.概述。并删除mysql_query()前面的@!例如:

$result = mysql_query($query); 

// Bail out on error 
if (!$result) 
    { 
    trigger_error("Database error: ". 
        mysql_error(). 
        " Query used was: ". 
        htmlentities($query), E_USER_ERROR); 
    die(); 
    } 

这将告诉你什么不顺心准确,并且正在使用什么最终的查询看起来像这样。

作为一个侧面说明(和可能,也解决你的根本原因)你看代码很容易受到SQL injection。这可能是什么打破了你的查询。

你需要做的mysql_real_escape_string()对所有传入值,像这样:

$formid = mysql_real_escape_string($_GET["token"]); 
+0

fixed!,我意识到我在查询中使用了错误的值,我应该使用 SELECT leadname, gasoiluserTemplate” 现在的工作!,谢谢你的头了与获得真正的逃避,我会改变这种状况太:-) – 2012-01-10 20:24:06

0

函数调用之前@被抑制应该告诉你发生了什么错误。