SHARE
TWEET

Untitled

a guest Feb 22nd, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. $conn = mysql_connect('MYSQL_HOST', 'MYSQL_USERNAME', 'MYSQL_PASSWORD') or die(mysql_error());
  4. mysql_select_db('MYSQL_DATABASE', $conn) or die(mysql_error($conn));
  5.  
  6.  
  7. $query = sprintf('SELECT * FROM MYSQL_TABLE');
  8. $result = mysql_query($query, $conn) or die(mysql_error($conn));
  9.  
  10.  
  11. header('Content-Type: text/csv');
  12. header('Content-Disposition: attachment;filename=export.csv');
  13.  
  14.  
  15. $row = mysql_fetch_assoc($result);
  16. if ($row) {
  17.     echocsv(array_keys($row));
  18. }
  19.  
  20.  
  21. while ($row) {
  22.     echocsv($row);
  23.     $row = mysql_fetch_assoc($result);
  24. }
  25.  
  26. function echocsv($fields)
  27. {
  28.     $separator = '';
  29.     foreach ($fields as $field) {
  30.         if (preg_match('/\\r|\\n|,|"/', $field)) {
  31.             $field = '"' . str_replace('"', '""', $field) . '"';
  32.         }
  33.         echo $separator . $field;
  34.         $separator = ',';
  35.     }
  36.     echo "\r\n";
  37. }
  38. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top