title: Loop through and download multiple CSVs from Database in PHP
body: <p>I have the following code which I am using to download a CSV of the data to a file called Out of Area.csv</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
  
  // START EXPORT OF LTHT DATA //
  exportMysqlToCsv('Out of Area.csv');
  
  
  // export csv
  function exportMysqlToCsv($filename = 'Out of Area.csv')
  {
  
     $conn = dbConnection();
  // Check connection
      if ($conn-&gt;connect_error) {
          die("Connection failed: " . $conn-&gt;connect_error);
      }
      $sql_query = "SELECT * FROM MDSData WHERE Downloaded IS NULL ORDER BY RegisteredCCG";
  
      // Gets the data from the database
      $result = $conn-&gt;query($sql_query);
  
      $f = fopen('php://temp', 'wt');
      $first = true;
      while ($row = $result-&gt;fetch_assoc()) {
          if ($first) {
              fputcsv($f, array_keys($row));
              $first = false;
          }
          fputcsv($f, $row);
      } // end while
  
  
  $sql_query = "UPDATE MDSData SET Downloaded='YES' WHERE Downloaded IS NULL";
  
      // Gets the data from the database
      if ($conn-&gt;query($sql_query) === TRUE) {
  } else {
  }
       $conn-&gt;close();
  
  
      $size = ftell($f);
      rewind($f);
  
      header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
      header("Content-Length: $size");
      // Output to browser with appropriate mime type, you choose ;)
      header("Content-type: text/x-csv");
      header("Content-type: text/csv");
      header("Content-type: application/csv");
      header("Content-Disposition: attachment; filename=$filename");
      fpassthru($f);
      exit;
  
  }
  
  
  
  // db connection function
  function dbConnection(){
      $servername = "servername";
      $username = "username";
      $password = "password";
      $dbname = "databasename";
      // Create connection
      $conn = new mysqli($servername, $username, $password, $dbname);
      return $conn;
  }
  
    header('location:csv-upload-mds.php?week='.$SelectedWeek);
  
  ?&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>However I have a column in my table called <strong>'TownName'</strong> and I would like to loop through creating a CSV Named <strong>'Out of Area - The Town Name Here.csv'</strong> for example  <strong>'Out of Area - Berkshire.csv'</strong></p>
  
  <p>Currently i've got to the stage where I can make it download 1 file with all the data in, however my issue comes when trying to download multiple files. Even if I duplicate the exportMysqlToCsv() call I get an error as this function can only be called once. So I need to put the loop inside an if statement I guess...</p>
  
  <p>My thoughts are something like the following</p>
  
  <pre><code>$sqlquery = "SELECT * FROM MDSData WHERE Downloaded IS NULL GROUP BY REGISTERED CCG"
  $result = $conn-&gt;query($sql_query);
  while ($row = $result-&gt;fetch_assoc()) {
  
  // Now I've looped through each CCG for each one I can set the file name and add the data to the file.
  
  $filename = "Out of Area - " . $row["TownName"]. ".csv";
  $sql_query = "SELECT * FROM MDSData WHERE Downloaded IS NULL ORDER BY RegisteredCCG";
  
  // Gets the data from the database
  $result = $conn-&gt;query($sql_query);
  
  $f = fopen('php://temp', 'wt');
  $first = true;
  while ($row = $result-&gt;fetch_assoc()) {
      if ($first) {
          fputcsv($f, array_keys($row));
          $first = false;
      }
      fputcsv($f, $row);
  } // end while of inside loop
  } // end while of 1st SQL query
  </code></pre>
  
  <p>However whilst I belive my idea is solid, I'm struggling to put it all together and looking for a little help.</p>
  
  <p>Thanks</p>