Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- set_time_limit(0);
- ini_set('memory_limit', '1G');
- ini_set("auto_detect_line_endings", true);
- ini_set('mysql.connect_timeout', 500);
- ini_set('default_socket_timeout', 500);
- include './config.php';
- $dbh = new PDO('mysql:host=...;dbname=...', '...', '...');
- $nome_arquivo = $_REQUEST["export_filename"];
- $file = $nome_arquivo . date("Ymd_His") . "";
- header("Content-type: text/csv");
- header("Content-Disposition: attachment; filename={$file}.csv");
- header("Pragma: no-cache");
- header("Expires: 0");
- // $sql = $_REQUEST["export_sql"];
- // $res = mysql_query($sql);
- // $row = mysql_fetch_assoc($res);
- // $data[0] = array_keys($row);
- // while ($row = mysql_fetch_assoc($res))
- // {
- // $data[] = array_values($row);
- // }
- $sql = $_REQUEST["export_sql"];
- $sth = $dbh->prepare($sql);
- $sth->execute();
- //$data[0] = $dbh->fetch( PDO::FETCH_ASSOC );
- $i = 1;
- while($row = $sth->fetch( PDO::FETCH_ASSOC )) {
- $data[$i++] = $row;
- }
- $data[0] = array_keys($data[1]);
- ksort($data);
- //debug($data);
- $output = fopen("php://output", "w");
- foreach ($data as &$val)
- {
- fputcsv($output, $val, ';', '"');
- }
- echo fgets($output);
- fclose($output);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement