2013-09-27 173 views
1

我正在創建腳本來自動導出我在MySQL中創建的查詢結果,並以電子郵件中的表格格式導出。我有它的工作到我收到2封電子郵件,一封電子郵件給我的錯誤:SQLSTATE [42000]:語法錯誤或訪問衝突:1064您的SQL語法中有錯誤; (*)作爲count,region,MONTHNAME(date)作爲month FROM tempur_stores.stats其中date> DATE_ADD(DATE(日期) NOW()),間隔-1 WEEK)和date < DATE(NOW())GROUP BY region,MONTH(date]我相信我的語法是正確的,因爲我得到預期的結果,每當我在SQL本身運行我的查詢。SQLSTATE [42000]:語法錯誤或訪問衝突:1064 PHP/MySQL

其他電子郵件只是有我在我的代碼指定的標題,計數區域

任何建議,我哪裏錯了/缺少的東西?

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 = '[email protected]'; 
    try { 
     $result = $stmt->execute()->as_array(); 
    } catch (Exception $e) { 
     mail('[email protected]', '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 <[email protected]>\n". 
    "Mailed-by: T UK <[email protected]>\n". 
    "Reply-To: T UK <[email protected]>\n". 
    "Return-Path: T UK <[email protected]>\n"; 

    mail($sendTo, $subject, implode('', $toEmail), $headers); 
    // mail('[email protected]', $subject, implode('', $toEmail), $headers); 

} 

回答

9

你在查詢中缺少一個右括號:

$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 
+0

我有一個'我查詢的結束? –

+0

感嘆......只是在該撇號前加一個括號**)**,它應該起作用。 – silkfire

+0

對不起!我的不好,我應該仔細閱讀!謝謝你的作品! –

相關問題