Advertisement
Guest User

Untitled

a guest
Aug 17th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. <?php
  2. // Database configuration
  3. $host = "localhost";
  4. $username = "mobstersonline_owner";
  5. $password = "Enterprise89";
  6. $database_name = "mobstersonline_mobstersonline";
  7.  
  8. // Get connection object and set the charset
  9. $conn = mysqli_connect($host, $username, $password, $database_name);
  10. $conn->set_charset("utf8");
  11.  
  12.  
  13. // Get All Table Names From the Database
  14. $tables = array();
  15. $sql = "SHOW TABLES";
  16. $result = mysqli_query($conn, $sql);
  17.  
  18. while ($row = mysqli_fetch_row($result)) {
  19. $tables[] = $row[0];
  20. }
  21. $result = mysqli_query($conn, $sql);
  22.  
  23. while ($row = mysqli_fetch_row($result)) {
  24. $tables[] = $row[0];
  25. }
  26. ?>
  27. <?php
  28. $sqlScript = "";
  29. foreach ($tables as $table) {
  30.  
  31. // Prepare SQLscript for creating table structure
  32. $query = "SHOW CREATE TABLE $table";
  33. $result = mysqli_query($conn, $query);
  34. $row = mysqli_fetch_row($result);
  35.  
  36. $sqlScript .= "\n\n" . $row[1] . ";\n\n";
  37.  
  38.  
  39. $query = "SELECT * FROM $table";
  40. $result = mysqli_query($conn, $query);
  41.  
  42. $columnCount = mysqli_num_fields($result);
  43.  
  44. // Prepare SQLscript for dumping data for each table
  45. for ($i = 0; $i < $columnCount; $i ++) {
  46. while ($row = mysqli_fetch_row($result)) {
  47. $sqlScript .= "INSERT INTO $table VALUES(";
  48. for ($j = 0; $j < $columnCount; $j ++) {
  49. $row[$j] = $row[$j];
  50.  
  51. if (isset($row[$j])) {
  52. $sqlScript .= '"' . $row[$j] . '"';
  53. } else {
  54. $sqlScript .= '""';
  55. }
  56. if ($j < ($columnCount - 1)) {
  57. $sqlScript .= ',';
  58. }
  59. }
  60. $sqlScript .= ");\n";
  61. }
  62. }
  63.  
  64. $sqlScript .= "\n";
  65. }
  66. ?>
  67. <?php
  68. if(!empty($sqlScript))
  69. {
  70. // Save the SQL script to a backup file
  71. $backup_file_name = $database_name . '_backup_' . time() . '.sql';
  72. $fileHandler = fopen($backup_file_name, 'w+');
  73. $number_of_lines = fwrite($fileHandler, $sqlScript);
  74. fclose($fileHandler);
  75.  
  76. }
  77. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement