×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Lori DK
Added: Nov 10, 2016 7:01 PM
Views: 18
Tags: export mysql php
  1. <?php include('settings.php');
  2.  
  3. $select = "SELECT * FROM listings";
  4.  
  5. $export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );
  6.  
  7. $fields = mysql_num_fields ( $export );
  8.  
  9. for ( $i = 0; $i < $fields; $i++ )
  10. {
  11.     $header .= mysql_field_name( $export , $i ) . "\t";
  12. }
  13.  
  14. while( $row = mysql_fetch_row( $export ) )
  15. {
  16.     $line = '';
  17.     foreach( $row as $value )
  18.     {
  19.         if ( ( !isset( $value ) ) || ( $value == "" ) )
  20.         {
  21.             $value = "\t";
  22.         }
  23.         else
  24.         {
  25.             $value = str_replace( '"' , '""' , $value );
  26.             $value = '"' . $value . '"' . "\t";
  27.         }
  28.         $line .= $value;
  29.     }
  30.     $data .= trim( $line ) . "\n";
  31. }
  32. $data = str_replace( "\r" , "" , $data );
  33.  
  34. if ( $data == "" )
  35. {
  36.     $data = "\n(0) Records Found!\n";
  37. }
  38.  
  39. header("Content-type: application/octet-stream");
  40. header("Content-Disposition: attachment; filename=listings.xls");
  41. header("Pragma: no-cache");
  42. header("Expires: 0");
  43. print "$header\n$data";
  44.  
  45. ?>