title: SQLSTATE[42000]: Syntax error or access violation: 1064 PHP/MySQL
body: <p>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: <strong>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 <code>count</code>,<code>region</code>, MONTHNAME(<code>date</code>) as<code>month</code> FROM tempur_stores.stats WHERE <code>date</code> > DATE_ADD(DATE(NOW()), INTERVAL -1 WEEK) AND <code>date</code> &lt; DATE(NOW()) GROUP BY <code>region</code>, MONTH(<code>date</code> ]</strong>  I am sure my syntax is correct as i get the expected results whenever i run my query in SQL itself.  </p>
  
  <p>The other email just has the headings i have specified in my code, <strong>count</strong>, <strong>region</strong> and <strong>month</strong></p>
  
  <p>Any suggestions where i am going wrong/missing something?</p>
  
  <p>PHP</p>
  
  <pre><code>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` &gt; DATE_ADD(DATE(NOW()), INTERVAL -1 WEEK) AND `date` &lt; DATE(NOW()) GROUP BY `region`, MONTH(`date`');
      $result = $stmt;
      $sendTo = 'myemail@live.com';
      try {
          $result = $stmt-&gt;execute()-&gt;as_array();
      } catch (Exception $e) {
          mail('myemail@live.com', 'Tempur 3rd Party Store Locator Searches', $e-&gt;getMessage());
      }
  
      $subject = 'Third Party Store Locator Searches - '.date("Y-m-d", strtotime("-1 week")).' - '.date("Y-m-d");
      if (count($result) &gt; 0) {
          $toEmail = array('
              &lt;html&gt;
              &lt;head&gt;
              &lt;title&gt;Third Party Store Locator Searches&lt;/title&gt;
              &lt;/head&gt;
              &lt;body&gt;
              &lt;table&gt;
                  &lt;tr&gt;
                      &lt;td&gt;Count&lt;/td&gt;
                      &lt;td&gt;Region&lt;/td&gt;
                      &lt;td&gt;Month&lt;/td&gt;
                  &lt;/tr&gt;
          ');
          foreach ($result as $row) {
              $toEmail[] = '
                  &lt;tr&gt;
                      &lt;td&gt;'.$row['count'].'&lt;/td&gt;
                      &lt;td&gt;'.$row['region'].'&lt;/td&gt;
                      &lt;td&gt;'.$row['month'].'&lt;/td&gt;
                  &lt;/tr&gt;
              ';
          }
          $toEmail[] = '
                  &lt;/table&gt;
                  &lt;/body&gt;
                  &lt;/html&gt;';
      } 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 &lt;no-reply@me.com&gt;\n".
      "Mailed-by: T UK &lt;no-reply@me.com&gt;\n".
      "Reply-To: T UK &lt;no-reply@me.com&gt;\n".
      "Return-Path: T UK &lt;no-reply@me.com&gt;\n";
  
      mail($sendTo, $subject, implode('', $toEmail), $headers);
     //   mail('myemail@live.com', $subject, implode('', $toEmail), $headers);
  
  }
  </code></pre>