Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //open database connection
- require ('../database-config.php');
- header('Content-Type: text/csv');
- header('Content-Disposition: attachment;filename=exported_archive_all.csv');
- //SQL Query for Data
- $sql = "SELECT * FROM inverter_data_archive;";
- //Prepare Query, Bind Parameters, Excute Query
- $STH = $dbh->prepare($sql);
- $STH->execute();
- //Export to .CSV
- $fp = fopen('php://output', 'w');
- // first set
- $first_row = $STH->fetch(PDO::FETCH_ASSOC);
- $headers = array_keys($first_row);
- fputcsv($fp, $headers); // put the headers
- fputcsv($fp, array_values($first_row)); // put the first row
- while ($row = $STH->fetch(PDO::FETCH_NUM)) {
- fputcsv($fp,$row); // push the rest
- }
- fclose($fp);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement