Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. <?php
  2. /* vars for export */
  3. // database record to be exported
  4. $db_record = 'manufacturing_';
  5. // optional where query
  6. $where = 'WHERE 1 ORDER BY 1';
  7. // filename for export
  8. $csv_filename = 'db_export_'.$db_record.'_'.date('Y-m-d').'.csv';
  9. // database variables
  10. $hostname = "localhost";
  11. $user = "XXXXXXXXX";
  12. $password = "XXXXXXXXX";
  13. $database = "XXXXXXXXX";
  14. // Database connecten voor alle services
  15. $connection = mysqli_connect($hostname, $user, $password) or die('Could not connect: ' . mysqli_error());
  16.  
  17. mysqli_select_db($connection, $database) or die ('Could not select database ' . mysqli_error());
  18. // create empty variable to be filled with export data
  19. $csv_export = '';
  20. // query to get data from database
  21. $query = mysqli_query($connection, "SELECT * FROM ".$db_record." ".$where);
  22. $field = mysqli_num_fields($connection, $query);
  23. // create line with field names
  24. for($i = 0; $i < $field; $i++) {
  25. $csv_export.= mysqli_field_name($connection, $query,$i).';';
  26. }
  27. // newline (seems to work both on Linux & Windows servers)
  28. $csv_export.= '
  29. ';
  30. // loop through database query and fill export variable
  31. while($row = mysqli_fetch_array($connection, $query)) {
  32. // create line with field values
  33. for($i = 0; $i < $field; $i++) {
  34. $csv_export.= '"'.$row[mysqli_field_name($connection, $query,$i)].'";';
  35. }
  36. $csv_export.= '
  37. ';
  38. }
  39. // Export the data and prompt a csv file for download
  40. header("Content-type: text/x-csv");
  41. header("Content-Disposition: attachment; filename=".$csv_filename."");
  42. echo($csv_export);
  43. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement