Hi all,
I'm trying to send emails to various people using a .php file run with a crontab. I'm having some PHP issues though.
My code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Email</title>
</head>
<body>
<?php # AUTOMATICALLY SENDS A REPORT TO EACH TEAM:
// Connect to the db:
require_once('../Master.php');
########################################################################################################
// Choose the subject of the report to be sent:
$reportname = "Test";
// Headers required to send html by email:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Choose who the report is being sent by:
$headers .= "From: test@thisemail.com" . "\r\n";
$TeamID = array();
// Create a query to select only the TeamIDs:
$q = "SELECT TeamID FROM Master_Team";
// Run the query:
$r = @mysqli_query($Master, $q);
// Fill the $TeamID array:
for($i = 0; $row = mysqli_fetch_array($r); $i++ )
{
$TeamID[i] = $row;
}
mysqli_free_result($r);
// Get the size of the $TeamID array:
$num = sizeof($TeamID);
// Loop through the $TeamID array to create the report for each one and send the email:
for($i = 0; $i != 1; $i++) // ------------------------------------------------------ Change "i != 1" to "i != $num"!!!
{
// Create a query to select the Team Name for the subject:
$q = "SELECT TeamName FROM Master_Team WHERE TeamID = \"{TeamID[i]}\"";
// Run the query:
$r = @mysqli_query($Master, $q);
$subject = "mysqli_fetch_array($r) . ' ' . $reportname";
mysqli_free_result($r);
################################################################################################
/*
// Create a query to get the TeamEMail:
$q = "SELECT TeamEMail FROM Master_Team WHERE TeamID = \"{$TeamID[i]}\"";
// Run the query:
$r = @mysqli_query($Master, $q);
// Assign the email to $to:
$to = "mysqli_fetch_array($r)";
mysqli_free_result($r);
*/
$to = "tinnin@example.co.uk"; // --------------------------------------------------------- Test Email!!!
################################################################################################
// Create a query to generate the report: ----------------------------------------------- EG: Player Goals for previous month
$q = "SELECT PlayerID, COUNT(*) AS Goals
FROM Master
WHERE TeamID = \"{TeamID[i]}\"
AND (
MONTH(GoalDate)= MONTH(SUBDATE(CURDATE(), INTERVAL 1 MONTH))
AND
YEAR(GoalDate)= YEAR(SUBDATE(CURDATE(), INTERVAL 1 MONTH))
)
GROUP BY PlayerID
ORDER BY Goals DESC";
// Run the query:
$r = @mysqli_query($Master, $q);
// If the query ran successfully create the email body in html/php:
if($r)
{
$body = '<html>
<head>
<title><?php echo $reportname; ?></title>
</head>
<body>
<?php
// Make the table headers:
echo \'<table align="center" cellspacing="3" cellpadding="3" width="100">
<tr>
<td align="left"><b>Player Name<b></td>
<td align="left"><b>Number of Goals<b></td>
</tr>\';
// Fetch the records for the report:
while($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
{
echo \'<tr>
<td align="left">\' .$row[\'PlayerID\']. \'</td>
<td align="left">\' .$row[\'Goals\']. \'</td>
</tr>\';
}
// Close the table:
echo \'</table>\';
?>
</body>
</html>';
################################################################################################
// Use the mail function to send the emails:
mail($to, $subject, $body, $headers);
}
mysqli_free_result($r);
}
?>
</body>
</html>
which is producing these errors:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in X on line 35
Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, null given in X on line 40
Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, null given in X on line 56
Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, null given in X on line 128
Where am I going wrong?
Thanks for any suggestions