title: Running a SQL query for each month - PHP
body: <p>Im looking for some help to run this query for each month.</p>
  
  <pre><code>$params = array(&amp;$_POST['query']);
  
  $SQL1 = "SELECT SUM(Income) AS OrderTotal 
  FROM SalesDB
  WHERE
  SalesDB.[Posting Date] &lt;= '2013-01-01' AND
  SalesDB.[Posting Date] &gt;= '2013-01-31'";
  
  $options = array("Scrollable" =&gt; SQLSRV_CURSOR_KEYSET);
  $getProducts1 = sqlsrv_query($conn, $SQL1, $params, $options);
  if ($getProducts1 === false)
  die( var_dump(sqlsrv_errors()) ); 
  
  while ($row = sqlsrv_fetch_array( $getProducts1, SQLSRV_FETCH_ASSOC)) 
  {
  $row['OrderTotal'];
  }
  </code></pre>
  
  <p>In this way, i get the Total income of January. Is there any way to do a loop, so it runs a query for each month in this year?</p>
  
  <p>So i don't have to do it this stupid way:</p>
  
  <pre><code>$params = array(&amp;$_POST['query']);
  
      $SQL1 = "SELECT SUM(Income) AS OrderTotal 
      FROM SalesDB
      WHERE
      SalesDB.[Posting Date] &lt;= '2013-01-01' AND
      SalesDB.[Posting Date] &gt;= '2013-01-31'";
  
      $options = array("Scrollable" =&gt; SQLSRV_CURSOR_KEYSET);
      $getProducts1 = sqlsrv_query($conn, $SQL1, $params, $options);
      if ($getProducts1 === false)
      die( var_dump(sqlsrv_errors()) ); 
  
  $SQL2 = "SELECT SUM(Income) AS OrderTotal 
      FROM SalesDB
      WHERE
      SalesDB.[Posting Date] &lt;= '2013-02-01' AND
      SalesDB.[Posting Date] &gt;= '2013-02-28'";
  
      $options = array("Scrollable" =&gt; SQLSRV_CURSOR_KEYSET);
      $getProducts2 = sqlsrv_query($conn, $SQL2, $params, $options);
      if ($getProducts2 === false)
      die( var_dump(sqlsrv_errors()) ); 
  
      while ($row1 = sqlsrv_fetch_array( $getProducts1, SQLSRV_FETCH_ASSOC) AND $row2 = sqlsrv_fetch_array( $getProducts2, SQLSRV_FETCH_ASSOC)) 
      {
      echo $row1['OrderTotal'];
  echo $row2['OrderTotal'];
  
      }
  </code></pre>