How to Restore MySQL Database using PHP

Getting Started

In the previous tutorial, we discussed on How to Backup MySQL Database using PHP. This time we are going to discuss on how to restore back up mysql databases using php. In order to beautify the presentation of this tutorial, I've used Bootstrap which is included in the downloadable of this tutorial but if you want, you can download Bootstrap using this link.

Creating our Restore Function

Next, we create our restore function that will restore our sql database and return status message. Create a new file, name it as function.php and paste the codes below.
  1. <?php
  2.  
  3. function restore($server, $username, $password, $dbname, $location){
  4. //connection
  5. $conn = new mysqli($server, $username, $password, $dbname);
  6.  
  7. //variable use to store queries from our sql file
  8. $sql = '';
  9.  
  10. //get our sql file
  11. $lines = file($location);
  12.  
  13. //return message
  14. $output = array('error'=>false);
  15.  
  16. //loop each line of our sql file
  17. foreach ($lines as $line){
  18.  
  19. //skip comments
  20. if(substr($line, 0, 2) == '--' || $line == ''){
  21. continue;
  22. }
  23.  
  24. //add each line to our query
  25. $sql .= $line;
  26.  
  27. //check if its the end of the line due to semicolon
  28. if (substr(trim($line), -1, 1) == ';'){
  29. //perform our query
  30. $query = $conn->query($sql);
  31. if(!$query){
  32. $output['error'] = true;
  33. $output['message'] = $conn->error;
  34. }
  35. else{
  36. $output['message'] = 'Database restored successfully';
  37. }
  38.  
  39. //reset our query variable
  40. $sql = '';
  41.  
  42. }
  43. }
  44.  
  45. return $output;
  46. }
  47.  
  48. ?>

Creating our Form

Normally, you can already use the function to restore the database but for the sake of presentation, we are going to create a form where you can input database credential, db name where you want to restore the back up sql and the back up sql. Create a new file, name it as index.php and paste the codes below.
  1. <?php session_start(); ?>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <title>How to Restore MySQL Database using PHP</title>
  7. <link rel="stylesheet" type="text/css" href="bootstrap4/css/bootstrap.min.css">
  8. </head>
  9. <body>
  10. <div class="container">
  11. <h1 class="text-center" style="margin-top:30px;">Restore MySQL Database using PHP</h1>
  12. <hr>
  13. <div class="row justify-content-center">
  14. <div class="col-sm-6">
  15. <?php
  16. if(isset($_SESSION['error'])){
  17. ?>
  18. <div class="alert alert-danger text-center">
  19. <?php echo $_SESSION['error']; ?>
  20. </div>
  21. <?php
  22.  
  23. unset($_SESSION['error']);
  24. }
  25.  
  26. if(isset($_SESSION['success'])){
  27. ?>
  28. <div class="alert alert-success text-center">
  29. <?php echo $_SESSION['success']; ?>
  30. </div>
  31. <?php
  32.  
  33. unset($_SESSION['success']);
  34. }
  35. ?>
  36. <div class="card">
  37. <div class="card-body">
  38. <h3>Database Credential and Name</h3>
  39. <br>
  40. <form method="POST" action="restore.php" enctype="multipart/form-data">
  41. <div class="form-group row">
  42. <label for="server" class="col-sm-3 col-form-label">Server</label>
  43. <div class="col-sm-9">
  44. <input type="text" class="form-control" id="server" name="server" placeholder="ex 'localhost'" required>
  45. </div>
  46. </div>
  47. <div class="form-group row">
  48. <label for="username" class="col-sm-3 col-form-label">Username</label>
  49. <div class="col-sm-9">
  50. <input type="text" class="form-control" id="username" name="username" placeholder="ex 'root'" required>
  51. </div>
  52. </div>
  53. <div class="form-group row">
  54. <label for="password" class="col-sm-3 col-form-label">Password</label>
  55. <div class="col-sm-9">
  56. <input type="text" class="form-control" id="password" name="password" placeholder="db password">
  57. </div>
  58. </div>
  59. <div class="form-group row">
  60. <label for="dbname" class="col-sm-3 col-form-label">Database</label>
  61. <div class="col-sm-9">
  62. <input type="text" class="form-control" id="dbname" name="dbname" placeholder="database you want to restore to" required>
  63. </div>
  64. </div>
  65. <div class="form-group row">
  66. <label for="sql" class="col-sm-3 col-form-label">File</label>
  67. <div class="col-sm-9">
  68. <input type="file" class="form-control-file" id="sql" name="sql" placeholder="database you want to restore to" required>
  69. </div>
  70. </div>
  71. <button type="submit" class="btn btn-primary" name="restore">Restore</button>
  72. </form>
  73. </div>
  74. </div>
  75. </div>
  76. </div>
  77. </div>
  78. </body>
  79. </html>

Creating our Restore Action

Lastly, we create the action if the form we created is submitted. This action will restore our back up sql. Create a new file, name it as restore.php and paste the codes below.
  1. <?php
  2.  
  3. //include our function
  4. include 'function.php';
  5.  
  6. if(isset($_POST['restore'])){
  7.  
  8. //get post data
  9. $server = $_POST['server'];
  10. $username = $_POST['username'];
  11. $password = $_POST['password'];
  12. $dbname = $_POST['dbname'];
  13.  
  14. //moving the uploaded sql file
  15. $filename = $_FILES['sql']['name'];
  16. move_uploaded_file($_FILES['sql']['tmp_name'],'upload/' . $filename);
  17. $file_location = 'upload/' . $filename;
  18.  
  19. //restore database using our function
  20. $restore = restore($server, $username, $password, $dbname, $file_location);
  21.  
  22. if($restore['error']){
  23. $_SESSION['error'] = $restore['message'];
  24. }
  25. else{
  26. $_SESSION['success'] = $restore['message'];
  27. }
  28.  
  29. }
  30. else{
  31. $_SESSION['error'] = 'Fill up credential first';
  32. }
  33.  
  34. header('location:index.php');
  35.  
  36. ?>
Note: Be sure to create upload folder. This is where our uploaded files are save. That ends this tutorial. Happy Coding :)

Comments

Submitted byRegin Romero (not verified)on Sun, 01/16/2022 - 03:22

ty sir/maam for your beautiful code its work perfectly in my capstone project

Add new comment