Import Database Tables Using PHP/MySQL

This tutorial will teach you on how create a script that restore or import database tables using PHP. The feature of this tutorial is it allows the user to restore or imports database backup without using the phpmyadmin panel. To understand more about this tutorial follow the steps bellow

Creating Our Database

First we are going to create our database which stores our data. To create a database: 1. Open phpmyadmin 2. Then create database and name it as "tutorials".

Creating Our Form

The code bellow will provide us the form where we can attach our sql file. Copy the link bellow and save it as “index.php”.
  1. <form action="import.php" method="POST" enctype="multipart/form-data">
  2. <input type="file" name="image" class="ed"><br />
  3. <input type="submit" value="Save" />
  4. </form>

Writing Our Script that Upload and Import our databse

The codes bellow includes our database connection, upload script and import to database script opy the code bellow and save it as “import.php”.
  1. <?php
  2.  
  3.  
  4. // new data
  5. $file=$_FILES['image']['tmp_name'];
  6. $image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
  7. $image_name= addslashes($_FILES['image']['name']);
  8. $image_size= getimagesize($_FILES['image']['tmp_name']);
  9.  
  10.  
  11. move_uploaded_file($_FILES["image"]["tmp_name"],"" . $_FILES["image"]["name"]);
  12.  
  13. $filename=$_FILES["image"]["name"];
  14.  
  15. $mysql_host = 'localhost';
  16. $mysql_username = 'root';
  17. $mysql_password = '';
  18. $mysql_database = 'tutorials';
  19.  
  20. // Connect to MySQL server
  21. mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());
  22. // Select database
  23. mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());
  24. mysql_query("SET NAMES 'utf8'");
  25. // Temporary variable, used to store current query
  26. $templine = '';
  27. // Read in entire file
  28. $lines = file($filename);
  29. // Loop through each line
  30. foreach ($lines as $line)
  31. {
  32. // Skip it if it's a comment
  33. if (substr($line, 0, 2) == '--' || $line == '')
  34. continue;
  35.  
  36. // Add this line to the current segment
  37. $templine .= $line;
  38. // If it has a semicolon at the end, it's the end of the query
  39. if (substr(trim($line), -1, 1) == ';')
  40. {
  41. // Perform the query
  42. mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
  43. // Reset temp variable to empty
  44. $templine = '';
  45. }
  46. }
  47. echo '<div style="text-align: center; margin-top: 50px;">';
  48. echo "Tables imported successfully<br>";
  49. $dir = dirname(__FILE__);
  50. echo 'form'.$dir.'/' .$filename .'To '.$mysql_database.'<br>';
  51. echo '<a href="../index.php">Back</a>';
  52. echo '</div>';
  53.  
  54.  
  55.  
  56. ?>
Note. Use the birthday.sql file attach together with this tutorial when you browse for database table. Hope this code will help you.

Add new comment