Advertisement
Guest User

Untitled

a guest
Oct 16th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. <?php
  2. $host = "localhost";
  3. $user = "root";
  4. $pass = "";
  5. $name = "intan";
  6.  
  7. backup_tables('$host','$user','$pass','$name');
  8. function backup_tables($host,$user,$pass,$name,$tables = '*')
  9. {
  10.  
  11. $link = mysql_connect($host,$user,$pass);
  12. mysql_select_db($name,$link);
  13.  
  14.  
  15. if($tables == '*')
  16. {
  17. $tables = array();
  18. $result = mysql_query('SHOW TABLES');
  19. while($row = mysql_fetch_row($result))
  20. {
  21. $tables[] = $row[0];
  22. }
  23. }
  24. else
  25. {
  26. $tables = is_array($tables) ? $tables : explode(',',$tables);
  27. }
  28.  
  29. foreach($tables as $table)
  30. {
  31. $result = mysql_query('SELECT * FROM '.$table);
  32. $num_fields = mysql_num_fields($result);
  33.  
  34. $return.= 'DROP TABLE '.$table.';';
  35. $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
  36. $return.= "\n\n".$row2[1].";\n\n";
  37.  
  38. for ($i = 0; $i < $num_fields; $i++)
  39. {
  40. while($row = mysql_fetch_row($result))
  41. {
  42. $return.= 'INSERT INTO '.$table.' VALUES(';
  43. for($j=0; $j < $num_fields; $j++)
  44. {
  45. $row[$j] = addslashes($row[$j]);
  46. $row[$j] = ereg_replace("\n","\\n",$row[$j]);
  47. if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
  48. if ($j < ($num_fields-1)) { $return.= ','; }
  49. }
  50. $return.= ");\n";
  51. }
  52. }
  53. $return.="\n\n\n";
  54. }
  55.  
  56. //save file
  57. $handle = fopen('a.sql','w+');
  58. fwrite($handle,$return);
  59. fclose($handle);
  60. }
  61. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement