Guest User

Untitled

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