Kar*_*rty 1 php mysql sql sql-server
I am creating a script to automatically export the results of a query i have created in MySQL, in a table format in an email. I have it working up to the point where i receive 2 emails, one email gives me the error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 [ SELECT COUNT(*) as count,region, MONTHNAME(date) asmonth FROM tempur_stores.stats WHERE date > DATE_ADD(DATE(NOW()), INTERVAL -1 WEEK) AND date < DATE(NOW()) GROUP BY region, MONTH(date ] I am sure my syntax is correct as i get the expected results whenever i run my query in SQL itself.
另一封电子邮件只有我在我的代码,计数,地区和月份中指定的标题
我有什么错误/遗失的建议吗?
PHP
public function action_third_party_searches()
{
$stmt = DB::query(Database::SELECT, 'SELECT COUNT(*) as `count`,`region`, MONTHNAME(`date`) as`month` FROM tempur_stores.stats WHERE `date` > DATE_ADD(DATE(NOW()), INTERVAL -1 WEEK) AND `date` < DATE(NOW()) GROUP BY `region`, MONTH(`date`');
$result = $stmt;
$sendTo = 'myemail@live.com';
try {
$result = $stmt->execute()->as_array();
} catch (Exception $e) {
mail('myemail@live.com', 'Tempur 3rd Party Store Locator Searches', $e->getMessage());
}
$subject = 'Third Party Store Locator Searches - '.date("Y-m-d", strtotime("-1 week")).' - '.date("Y-m-d");
if (count($result) > 0) {
$toEmail = array('
<html>
<head>
<title>Third Party Store Locator Searches</title>
</head>
<body>
<table>
<tr>
<td>Count</td>
<td>Region</td>
<td>Month</td>
</tr>
');
foreach ($result as $row) {
$toEmail[] = '
<tr>
<td>'.$row['count'].'</td>
<td>'.$row['region'].'</td>
<td>'.$row['month'].'</td>
</tr>
';
}
$toEmail[] = '
</table>
</body>
</html>';
} else {
$toEmail = array('No searches were taken last week!');
}
$headers = "Content-type: text/html; charset=utf-8\n".
"X-Mailer: PHP/".phpversion()."\n".
"MIME-Version: 1.0\n".
"From: T UK <no-reply@me.com>\n".
"Mailed-by: T UK <no-reply@me.com>\n".
"Reply-To: T UK <no-reply@me.com>\n".
"Return-Path: T UK <no-reply@me.com>\n";
mail($sendTo, $subject, implode('', $toEmail), $headers);
// mail('myemail@live.com', $subject, implode('', $toEmail), $headers);
}
Run Code Online (Sandbox Code Playgroud)
您在查询中缺少右括号:
$stmt = DB::query(Database::SELECT, 'SELECT COUNT(*) as `count`,`region`, MONTHNAME(`date`) as`month`
FROM tempur_stores.stats
WHERE `date` > DATE_ADD(DATE(NOW()), INTERVAL -1 WEEK)
AND `date` < DATE(NOW())
GROUP BY `region`, MONTH(`date`');
---------- ^ right there
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39639 次 |
| 最近记录: |