Advertisement
Guest User

Untitled

a guest
Sep 8th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.19 KB | None | 0 0
  1. /*Snippet Name: Export table to Excel or MS Word file
  2.  
  3. Description: Export a mySQL database table to an EXCEL file.
  4. database table dump to WORD document possible also.
  5. */
  6.  
  7. <?PHP
  8.  
  9. //EDIT YOUR MySQL Connection Info:
  10. $DB_Server = "";        //your MySQL Server
  11. $DB_Username = "";                 //your MySQL User Name
  12. $DB_Password = "";                //your MySQL Password
  13. $DB_DBName = "";                //your MySQL Database Name
  14. $DB_TBLName = "";                //your MySQL Table Name
  15.  
  16. //$DB_TBLName,  $DB_DBName, may also be commented out & passed to the browser
  17. //as parameters in a query string, so that this code may be easily reused for
  18. //any MySQL table or any MySQL database on your server
  19.  
  20. //DEFINE SQL QUERY:
  21. //edit this to suit your needs
  22. $sql = "Select * from $DB_TBLName";
  23.  
  24. //Optional: print out title to top of Excel or Word file with Timestamp
  25. //for when file was generated:
  26. //set $Use_Titel = 1 to generate title, 0 not to use title
  27. $Use_Title = 1;
  28. //define date for title: EDIT this to create the time-format you need
  29. $now_date = DATE('m-d-Y H:i');
  30. //define title for .doc or .xls file: EDIT this if you want
  31. $title = "Dump For Table $DB_TBLName from Database $DB_DBName on $now_date";
  32. /*
  33.  
  34. Leave the connection info below as it is:
  35. just edit the above.
  36.  
  37. (Editing of code past this point recommended only for advanced users.)
  38. */
  39. //create MySQL connection
  40. $Connect = @MYSQL_CONNECT($DB_Server, $DB_Username, $DB_Password)
  41.      or DIE("Couldn't connect to MySQL:<br>" . MYSQL_ERROR() . "<br>" . MYSQL_ERRNO());
  42. //select database
  43. $Db = @MYSQL_SELECT_DB($DB_DBName, $Connect)
  44.      or DIE("Couldn't select database:<br>" . MYSQL_ERROR(). "<br>" . MYSQL_ERRNO());
  45. //execute query
  46. $result = @MYSQL_QUERY($sql,$Connect)
  47.      or DIE("Couldn't execute query:<br>" . MYSQL_ERROR(). "<br>" . MYSQL_ERRNO());
  48.  
  49. //if this parameter is included ($w=1), file returned will be in word format ('.doc')
  50. //if parameter is not included, file returned will be in excel format ('.xls')
  51. IF (ISSET($w) && ($w==1))
  52. {
  53.      $file_type = "msword";
  54.      $file_ending = "doc";
  55. }ELSE {
  56.      $file_type = "vnd.ms-excel";
  57.      $file_ending = "xls";
  58. }
  59. //header info for browser: determines file type ('.doc' or '.xls')
  60. HEADER("Content-Type: application/$file_type");
  61. HEADER("Content-Disposition: attachment; filename=database_dump.$file_ending");
  62. HEADER("Pragma: no-cache");
  63. HEADER("Expires: 0");
  64.  
  65. /*    Start of Formatting for Word or Excel    */
  66.  
  67. IF (ISSET($w) && ($w==1)) //check for $w again
  68. {
  69.      /*    FORMATTING FOR WORD DOCUMENTS ('.doc')   */
  70.      //create title with timestamp:
  71.      IF ($Use_Title == 1)
  72.      {
  73.          ECHO("$title\n\n");
  74.      }
  75.      //define separator (defines columns in excel & tabs in word)
  76.      $sep = "\n"; //new line character
  77.  
  78.      WHILE($row = MYSQL_FETCH_ROW($result))
  79.      {
  80.          //set_time_limit(60); // HaRa
  81.          $schema_insert = "";
  82.          FOR($j=0; $j<mysql_num_fields($result);$j++)
  83.          {
  84.          //define field names
  85.          $field_name = MYSQL_FIELD_NAME($result,$j);
  86.          //will show name of fields
  87.          $schema_insert .= "$field_name:\t";
  88.              IF(!ISSET($row[$j])) {
  89.                  $schema_insert .= "NULL".$sep;
  90.                  }
  91.              ELSEIF ($row[$j] != "") {
  92.                  $schema_insert .= "$row[$j]".$sep;
  93.                  }
  94.              ELSE {
  95.                  $schema_insert .= "".$sep;
  96.                  }
  97.          }
  98.          $schema_insert = STR_REPLACE($sep."$", "", $schema_insert);
  99.          $schema_insert .= "\t";
  100.          PRINT(TRIM($schema_insert));
  101.          //end of each mysql row
  102.          //creates line to separate data from each MySQL table row
  103.          PRINT "\n----------------------------------------------------\n";
  104.      }
  105. }ELSE{
  106.      /*    FORMATTING FOR EXCEL DOCUMENTS ('.xls')   */
  107.      //create title with timestamp:
  108.      IF ($Use_Title == 1)
  109.      {
  110.          ECHO("$title\n");
  111.      }
  112.      //define separator (defines columns in excel & tabs in word)
  113.      $sep = "\t"; //tabbed character
  114.  
  115.      //start of printing column names as names of MySQL fields
  116.      FOR ($i = 0; $i < MYSQL_NUM_FIELDS($result); $i++)
  117.      {
  118.          ECHO MYSQL_FIELD_NAME($result,$i) . "\t";
  119.      }
  120.      PRINT("\n");
  121.      //end of printing column names
  122.  
  123.      //start while loop to get data
  124.      WHILE($row = MYSQL_FETCH_ROW($result))
  125.      {
  126.          //set_time_limit(60); // HaRa
  127.          $schema_insert = "";
  128.          FOR($j=0; $j<mysql_num_fields($result);$j++)
  129.          {
  130.              IF(!ISSET($row[$j]))
  131.                  $schema_insert .= "NULL".$sep;
  132.              ELSEIF ($row[$j] != "")
  133.                  $schema_insert .= "$row[$j]".$sep;
  134.              ELSE
  135.                  $schema_insert .= "".$sep;
  136.          }
  137.          $schema_insert = STR_REPLACE($sep."$", "", $schema_insert);
  138.          //following fix suggested by Josue (thanks, Josue!)
  139.          //this corrects output in excel when table fields contain \n or \r
  140.          //these two characters are now replaced with a space
  141.          $schema_insert = PREG_REPLACE("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
  142.          $schema_insert .= "\t";
  143.          PRINT(TRIM($schema_insert));
  144.          PRINT "\n";
  145.      }
  146. }
  147.  
  148. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement