Guest User

Untitled

a guest
May 27th, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.66 KB | None | 0 0
  1. <?php
  2. set_time_limit(0);
  3. $mysqldump_version="1.02";
  4.  
  5. $print_form=1;
  6. $output_messages=array();
  7.  
  8.  
  9. //test mysql connection
  10. if( isset($_REQUEST['action']) )
  11. {
  12. $mysql_host=$_REQUEST['mysql_host'];
  13. $mysql_database=$_REQUEST['mysql_database'];
  14. $mysql_username=$_REQUEST['mysql_username'];
  15. $mysql_password=$_REQUEST['mysql_password'];
  16.  
  17. if( 'Test Connection' == $_REQUEST['action'])
  18. {
  19. _mysql_test($mysql_host,$mysql_database, $mysql_username, $mysql_password);
  20. }
  21. else if( 'Export' == $_REQUEST['action'])
  22. {
  23. _mysql_test($mysql_host,$mysql_database, $mysql_username, $mysql_password);
  24. if( 'SQL' == $_REQUEST['output_format'] )
  25. {
  26. $print_form=0;
  27.  
  28. //ob_start("ob_gzhandler");
  29. header('Content-type: text/plain');
  30. header('Content-Disposition: attachment; filename="'.$mysql_host."_".$mysql_database."_".date('YmdHis').'.sql"');
  31. echo "/*mysqldump.php version $mysqldump_version */\n";
  32. _mysqldump($mysql_database);
  33.  
  34. //header("Content-Length: ".ob_get_length());
  35.  
  36. //ob_end_flush();
  37. }
  38. else if( 'CSV' == $_REQUEST['output_format'] && isset($_REQUEST['mysql_table']))
  39. {
  40. $print_form=0;
  41.  
  42. ob_start("ob_gzhandler");
  43.  
  44. header('Content-type: text/comma-separated-values');
  45. header('Content-Disposition: attachment; filename="'.$mysql_host."_".$mysql_database."_".$mysql_table."_".date('YmdHis').'.csv"');
  46. //header('Content-type: text/plain');
  47. _mysqldump_csv($_REQUEST['mysql_table']);
  48. header("Content-Length: ".ob_get_length());
  49. ob_end_flush();
  50. }
  51. }
  52.  
  53. }
  54.  
  55. function _mysqldump_csv($table)
  56. {
  57. $delimiter= ",";
  58. if( isset($_REQUEST['csv_delimiter']))
  59. $delimiter= $_REQUEST['csv_delimiter'];
  60.  
  61. if( 'Tab' == $delimiter)
  62. $delimiter="\t";
  63.  
  64.  
  65. $sql="select * from `$table`;";
  66. $result=mysql_query($sql);
  67. if( $result)
  68. {
  69. $num_rows= mysql_num_rows($result);
  70. $num_fields= mysql_num_fields($result);
  71.  
  72. $i=0;
  73. while( $i < $num_fields)
  74. {
  75. $meta= mysql_fetch_field($result, $i);
  76. echo($meta->name);
  77. if( $i < $num_fields-1)
  78. echo "$delimiter";
  79. $i++;
  80. }
  81. echo "\n";
  82.  
  83. if( $num_rows > 0)
  84. {
  85. while( $row= mysql_fetch_row($result))
  86. {
  87. for( $i=0; $i < $num_fields; $i++)
  88. {
  89. echo mysql_real_escape_string($row[$i]);
  90. if( $i < $num_fields-1)
  91. echo "$delimiter";
  92. }
  93. echo "\n";
  94. }
  95.  
  96. }
  97. }
  98. mysql_free_result($result);
  99.  
  100. }
  101.  
  102.  
  103. function _mysqldump($mysql_database)
  104. {
  105. $sql="show tables;";
  106. $result= mysql_query($sql);
  107. if( $result)
  108. {
  109. while( $row= mysql_fetch_row($result))
  110. {
  111. _mysqldump_table_structure($row[0]);
  112.  
  113. if( isset($_REQUEST['sql_table_data']))
  114. {
  115. _mysqldump_table_data($row[0]);
  116. }
  117. }
  118. }
  119. else
  120. {
  121. echo "/* no tables in $mysql_database */\n";
  122. }
  123. mysql_free_result($result);
  124. }
  125.  
  126. function _mysqldump_table_structure($table)
  127. {
  128. echo "/* Table structure for table `$table` */\n";
  129. if( isset($_REQUEST['sql_drop_table']))
  130. {
  131. echo "DROP TABLE IF EXISTS `$table`;\n\n";
  132. }
  133. if( isset($_REQUEST['sql_create_table']))
  134. {
  135.  
  136. $sql="show create table `$table`; ";
  137. $result=mysql_query($sql);
  138. if( $result)
  139. {
  140. if($row= mysql_fetch_assoc($result))
  141. {
  142. echo $row['Create Table'].";\n\n";
  143. }
  144. }
  145. mysql_free_result($result);
  146. }
  147. }
  148.  
  149. function _mysqldump_table_data($table)
  150. {
  151.  
  152. $sql="select * from `$table`;";
  153. $result=mysql_query($sql);
  154. if( $result)
  155. {
  156. $num_rows= mysql_num_rows($result);
  157. $num_fields= mysql_num_fields($result);
  158.  
  159. if( $num_rows > 0)
  160. {
  161. echo "/* dumping data for table `$table` */\n";
  162.  
  163. $field_type=array();
  164. $i=0;
  165. while( $i < $num_fields)
  166. {
  167. $meta= mysql_fetch_field($result, $i);
  168. array_push($field_type, $meta->type);
  169. $i++;
  170. }
  171.  
  172. //print_r( $field_type);
  173. echo "insert into `$table` values\n";
  174. $index=0;
  175. while( $row= mysql_fetch_row($result))
  176. {
  177. echo "(";
  178. for( $i=0; $i < $num_fields; $i++)
  179. {
  180. if( is_null( $row[$i]))
  181. echo "null";
  182. else
  183. {
  184. switch( $field_type[$i])
  185. {
  186. case 'int':
  187. echo $row[$i];
  188. break;
  189. case 'string':
  190. case 'blob' :
  191. default:
  192. echo "'".mysql_real_escape_string($row[$i])."'";
  193.  
  194. }
  195. }
  196. if( $i < $num_fields-1)
  197. echo ",";
  198. }
  199. echo ")";
  200.  
  201. if( $index < $num_rows-1)
  202. echo ",";
  203. else
  204. echo ";";
  205. echo "\n";
  206.  
  207. $index++;
  208. }
  209. }
  210. }
  211. mysql_free_result($result);
  212. echo "\n";
  213. }
  214.  
  215. function _mysql_test($mysql_host,$mysql_database, $mysql_username, $mysql_password)
  216. {
  217. global $output_messages;
  218. $link = mysql_connect($mysql_host, $mysql_username, $mysql_password);
  219. if (!$link)
  220. {
  221. array_push($output_messages, 'Could not connect: ' . mysql_error());
  222. }
  223. else
  224. {
  225. array_push ($output_messages,"Connected with MySQL server:$mysql_username@$mysql_host successfully");
  226.  
  227. $db_selected = mysql_select_db($mysql_database, $link);
  228. if (!$db_selected)
  229. {
  230. array_push ($output_messages,'Can\'t use $mysql_database : ' . mysql_error());
  231. }
  232. else
  233. array_push ($output_messages,"Connected with MySQL database:$mysql_database successfully");
  234. }
  235.  
  236. }
  237.  
  238. if( $print_form >0 )
  239. {
  240. ?>
  241. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  242.  
  243. <html>
  244. <head>
  245. <title>mysqldump.php version <?php echo $mysqldump_version; ?></title>
  246. </head>
  247.  
  248. <body>
  249. <?php
  250. foreach ($output_messages as $message)
  251. {
  252. echo $message."<br />";
  253. }
  254. ?>
  255. <form action="" method="post">
  256. MySQL connection parameters:
  257. <table border="0">
  258. <tr>
  259. <td>Host:</td>
  260. <td><input name="mysql_host" value="<?php if(isset($_REQUEST['mysql_host']))echo $_REQUEST['mysql_host']; else echo 'localhost';?>" /></td>
  261. </tr>
  262. <tr>
  263. <td>Database:</td>
  264. <td><input name="mysql_database" value="<?php echo $_REQUEST['mysql_database']; ?>" /></td>
  265. </tr>
  266. <tr>
  267. <td>Username:</td>
  268. <td><input name="mysql_username" value="<?php echo $_REQUEST['mysql_username']; ?>" /></td>
  269. </tr>
  270. <tr>
  271. <td>Password:</td>
  272. <td><input type="password" name="mysql_password" value="<?php echo $_REQUEST['mysql_password']; ?>" /></td>
  273. </tr>
  274. <tr>
  275. <td>Output format: </td>
  276. <td>
  277. <select name="output_format" >
  278. <option value="SQL" <?php if( isset($_REQUEST['output_format']) && 'SQL' == $_REQUEST['output_format']) echo "selected";?> >SQL</option>
  279. <option value="CSV" <?php if( isset($_REQUEST['output_format']) && 'CSV' == $_REQUEST['output_format']) echo "selected";?> >CSV</option>
  280.  
  281. </select>
  282. </td>
  283. </tr>
  284. </table>
  285. <input type="submit" name="action" value="Test Connection"><br />
  286.  
  287. <br>Dump options(SQL):
  288. <table border="0">
  289.  
  290. <tr>
  291. <td>Drop table statement: </td>
  292. <td><input type="checkbox" name="sql_drop_table" <?php if(isset($_REQUEST['action']) && ! isset($_REQUEST['sql_drop_table'])) ; else echo 'checked' ?> /></td>
  293. </tr>
  294. <tr>
  295. <td>Create table statement: </td>
  296. <td><input type="checkbox" name="sql_create_table" <?php if(isset($_REQUEST['action']) && ! isset($_REQUEST['sql_create_table'])) ; else echo 'checked' ?> /></td>
  297. </tr>
  298. <tr>
  299. <td>Table data: </td>
  300. <td><input type="checkbox" name="sql_table_data" <?php if(isset($_REQUEST['action']) && ! isset($_REQUEST['sql_table_data'])) ; else echo 'checked' ?>/></td>
  301. </tr>
  302. </table>
  303. <br>Dump options(CSV):
  304. <table border="0">
  305. <tr>
  306. <td>Delimiter:</td>
  307. <td><select name="csv_delimiter">
  308. <option value="," <?php if( isset($_REQUEST['output_format']) && ',' == $_REQUEST['output_format']) echo "selected";?>>,</option>
  309. <option value="Tab" <?php if( isset($_REQUEST['output_format']) && 'Tab' == $_REQUEST['output_format']) echo "selected";?>>Tab</option>
  310. <option value="|" <?php if( isset($_REQUEST['output_format']) && '|' == $_REQUEST['output_format']) echo "selected";?>>|</option>
  311. </select>
  312. </td>
  313. </tr>
  314. <tr>
  315. <td>Table:</td>
  316. <td><input type="input" name="mysql_table" value="<?php echo $_REQUEST['mysql_table']; ?>" /></td>
  317. </tr>
  318. <tr>
  319. <td>Header: </td>
  320. <td><input type="checkbox" name="csv_header" <?php if(isset($_REQUEST['action']) && ! isset($_REQUEST['csv_header'])) ; else echo 'checked' ?>/></td>
  321. </tr>
  322. </table>
  323.  
  324.  
  325. <input type="submit" name="action" value="Export"><br />
  326. </form>
  327. </body>
  328. </html>
  329.  
  330. <?php
  331. }
  332. ?>
Add Comment
Please, Sign In to add comment