Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. <?php
  2.  
  3. // The name of the table
  4. $db_record = '';
  5. // optional where query
  6. $where = 'WHERE 1 ORDER BY 1';
  7.  
  8. $csv_filename = 'db_export_'.$db_record.'_'.date('Y-m-d').'.csv';
  9.  
  10. $hostname = "";
  11. $user = "";
  12. $password = "";
  13. $database = "";
  14. $port = ;
  15.  
  16. $conn = mysqli_connect($hostname, $user, $password, $database, $port);
  17. if (mysqli_connect_errno()) {
  18. die("Failed to connect to MySQL: " . mysqli_connect_error());
  19. }
  20.  
  21. $csv_export = '';
  22.  
  23. $query = mysqli_query($conn, "SELECT * FROM ".$db_record." ".$where);
  24. $field = mysqli_field_count($conn);
  25.  
  26. for($i = 0; $i < $field; $i++) {
  27. $csv_export.= mysqli_fetch_field_direct($query, $i)->name.';';
  28. }
  29.  
  30. $csv_export.= '
  31. ';
  32.  
  33. while($row = mysqli_fetch_array($query)) {
  34. // create line with field values
  35. for($i = 0; $i < $field; $i++) {
  36. $csv_export.= '"'.$row[mysqli_fetch_field_direct($query, $i)->name].'";';
  37. }
  38. $csv_export.= '
  39. ';
  40. }
  41.  
  42. header("Content-type: text/x-csv");
  43. header("Content-Disposition: attachment; filename=".$csv_filename."");
  44. echo($csv_export);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement