phpmailer抛出一个mysql错误

时间:2012-01-10 19:58:27

标签: php mysql phpmailer

我正在尝试使用phpmailer发送简报,但每次尝试触发时都会出现以下错误。我不确定我的sql语法是否正确?

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('crea@cruiseit.co.uk', 'List manager');
$mail->AddReplyTo('crea@cruiseit.co.uk', '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();
}
?>

编辑&gt;&gt;&gt;&gt;&gt;&gt;

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

            <?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('crea@cruiseit.co.uk', 'List manager');
            $mail->AddReplyTo('crea@cruiseit.co.uk', '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();
            }
            ?>

2 个答案:

答案 0 :(得分: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"]);

答案 1 :(得分:0)

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