Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. <?php
  2. //open database connection
  3. require ('../database-config.php');
  4. header('Content-Type: text/csv');
  5. header('Content-Disposition: attachment;filename=exported_archive_all.csv');
  6.  
  7. //SQL Query for Data
  8. $sql = "SELECT * FROM inverter_data_archive;";
  9. //Prepare Query, Bind Parameters, Excute Query
  10. $STH = $dbh->prepare($sql);
  11. $STH->execute();
  12.  
  13. //Export to .CSV
  14. $fp = fopen('php://output', 'w');
  15.  
  16. // first set
  17. $first_row = $STH->fetch(PDO::FETCH_ASSOC);
  18. $headers = array_keys($first_row);
  19. fputcsv($fp, $headers); // put the headers
  20. fputcsv($fp, array_values($first_row)); // put the first row
  21.  
  22. while ($row = $STH->fetch(PDO::FETCH_NUM)) {
  23. fputcsv($fp,$row); // push the rest
  24. }
  25. fclose($fp);
  26. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement