Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. <?php
  2. set_time_limit(0);
  3. ini_set('memory_limit', '1G');
  4. ini_set("auto_detect_line_endings", true);
  5. ini_set('mysql.connect_timeout', 500);
  6. ini_set('default_socket_timeout', 500);
  7.  
  8. include './config.php';
  9.  
  10. $dbh = new PDO('mysql:host=...;dbname=...', '...', '...');
  11.  
  12.  
  13. $nome_arquivo = $_REQUEST["export_filename"];
  14. $file = $nome_arquivo . date("Ymd_His") . "";
  15.  
  16. header("Content-type: text/csv");
  17. header("Content-Disposition: attachment; filename={$file}.csv");
  18. header("Pragma: no-cache");
  19. header("Expires: 0");
  20.  
  21. // $sql = $_REQUEST["export_sql"];
  22. // $res = mysql_query($sql);
  23. // $row = mysql_fetch_assoc($res);
  24. // $data[0] = array_keys($row);
  25.  
  26. // while ($row = mysql_fetch_assoc($res))
  27. // {
  28. // $data[] = array_values($row);
  29. // }
  30.  
  31. $sql = $_REQUEST["export_sql"];
  32.  
  33. $sth = $dbh->prepare($sql);
  34.  
  35. $sth->execute();
  36.  
  37. //$data[0] = $dbh->fetch( PDO::FETCH_ASSOC );
  38.  
  39. $i = 1;
  40. while($row = $sth->fetch( PDO::FETCH_ASSOC )) {
  41. $data[$i++] = $row;
  42. }
  43. $data[0] = array_keys($data[1]);
  44.  
  45. ksort($data);
  46.  
  47. //debug($data);
  48.  
  49.  
  50. $output = fopen("php://output", "w");
  51. foreach ($data as &$val)
  52. {
  53. fputcsv($output, $val, ';', '"');
  54. }
  55.  
  56. echo fgets($output);
  57.  
  58. fclose($output);
  59.  
  60.  
  61. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement