Advertisement
Guest User

Untitled

a guest
May 24th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1.  
  2.  
  3. try {
  4.  
  5. $mysqli = new mysqli($h, $l, $p, $b);
  6.  
  7.  
  8. if ($mysqli->connect_errno) {
  9. throw new Exception("Failed to connect to MySQL: " . $mysqli->connect_error);
  10. }
  11.  
  12. $f_output = fopen("/media/cdrom/up", 'w');
  13. $strSQL = 'SHOW TABLES';
  14. if (!$res_tables = $mysqli->query($strSQL))
  15. throw new Exception("MySQL Error: " . $mysqli->error . 'SQL: '.$strSQL);
  16.  
  17. while($row = $res_tables->fetch_array()) {
  18. $aTables[] = $row[0];
  19. }
  20.  
  21. $res_tables->free();
  22.  
  23. foreach($aTables as $table)
  24. {
  25. fwrite($f_output,"-- --------------------------------------------------------\n");
  26. fwrite($f_output,"-- Structure for '". $table."'\n");
  27. fwrite($f_output,"--\n\n");
  28.  
  29. fwrite($f_output,'DROP TABLE IF EXISTS '.$table.';');
  30.  
  31. // ask MySQL how to create the table
  32. $strSQL = 'SHOW CREATE TABLE '.$table;
  33. if (!$res_create = $mysqli->query($strSQL))
  34. throw new Exception("MySQL Error: " . $mysqli->error . 'SQL: '.$strSQL);
  35. $row_create = $res_create->fetch_assoc();
  36.  
  37. fwrite($f_output,"\n".$row_create['Create Table'].";\n");
  38.  
  39.  
  40. fwrite($f_output,"-- --------------------------------------------------------\n");
  41. fwrite($f_output,'-- Dump Data for `'. $table."`\n");
  42. fwrite($f_output,"--\n\n");
  43. $res_create->free();
  44.  
  45. $strSQL = 'SELECT * FROM '.$table;
  46. if (!$res_select = $mysqli->query($strSQL))
  47. throw new Exception("MySQL Error: " . $mysqli->error . 'SQL: '.$strSQL);
  48.  
  49.  
  50. $fields_info = $res_select->fetch_fields();
  51.  
  52. $strFields = '';
  53. foreach ($fields_info as $field) {
  54. if ($strFields != '') $strFields .= ',';
  55. $strFields .= "`".$field->name."`";
  56. }
  57.  
  58. fwrite($f_output,"INSERT INTO ".$table." (".$strFields.") VALUES ");
  59. $qinsert = "";
  60. $i = 0;
  61. while ($values = $res_select->fetch_assoc()) {
  62. set_time_limit(600);
  63. $strFields = '';
  64. $strValues = '';
  65. foreach ($fields_info as $field) {
  66. if ($strFields != '') $strFields .= ',';
  67. $strFields .= "`".$field->name."`";
  68. if ($strValues != '') $strValues .= ',';
  69. $strValues .= '"'.mysql_real_escape_string($mysqli,$values[$field->name].'"';
  70. }
  71.  
  72. fwrite($f_output,"$qinsert\n(".$strValues.")");
  73.  
  74. if($i > 290) { $qinsert = ";\nINSERT INTO ".$table." (".$strFields.") VALUES "; $i = 0; }
  75. else $qinsert = ", ";
  76. $i++;
  77. }
  78. fwrite($f_output,";\n\n\n");
  79.  
  80. $res_select->free();
  81.  
  82. }
  83.  
  84.  
  85. } catch (Exception $e) {
  86. fwrite($f_output,$e->getMessage());
  87. }
  88.  
  89.  
  90. fclose($f_output);
  91.  
  92. $mysqli->close();
  93.  
  94.  
  95. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement