【发布时间】:2012-01-10 19:58:27
【问题描述】:
我正在尝试使用 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("@", "@", $row["email"]) . ') ' . $mail->ErrorInfo . '<br>';
} else {
echo "Message sent to :" . $row["full_name"] . ' (' . str_replace("@", "@", $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('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("@", "@", $row["email"]) . ') ' . $mail->ErrorInfo . '<br>';
} else {
echo "Message sent to :" . $row["full_name"] . ' (' . str_replace("@", "@", $row["email"]) . ')<br>';
}
// Clear all addresses and attachments for next loop
$mail->ClearAddresses();
$mail->ClearAttachments();
}
?>
【问题讨论】:
-
你为什么使用@MYSQL_QUERY
-
尝试删除 MYSQL_QUERY($query) 行前面的 @ 符号,看看它在做什么。您很可能会隐藏一条错误消息,该消息会更详细地告诉您出了什么问题。