Import SQL file in MySQL Database using PHP


Welcome back to , in our previous post we learn how to export mysql database using php.

Today we will learn how to import a SQL file in our database with the help of our PHP script.

there are two way to import a sql file in database.

Import SQL file in MySQL Database using PHP

    1. we can import .sql file  with the help of command line.
      mysql -u username -p database_name < filename.sql

      2.  The second way to import MySQL databases through phpMyAdmin.

      Also Read : How to create a facebook like chat system using PHP ,AJAX and MySQL.

      Send Mail Without SMTP Authentication in PHP.

      Create a dynamic progress-bar using PHP.

      Generate Enrollment-number using PHP.

      Decode JSON format using PHP function


      $conn =mysqli_connect('localhost', 'root', '' , 'dump');
      $query = '';
      $sqlScript = file('find_backup_1538046010.sql');
      foreach ($sqlScript as $line) {
        $startWith = substr(trim($line), 0 ,2);
        $endWith = substr(trim($line), -1 ,1);
        if (empty($line) || $startWith == '--' || $startWith == '/*' || $startWith == '//') {
          $query = $query . $line;
        if ($endWith == ';') {
          mysqli_query($conn,$query) or die('<div class="error-response sql-import-response">Problem in executing the SQL query <b>' . $query. '</b></div>');
          $query= '';   
      echo '<div class="success-response sql-import-response">SQL file imported successfully</div>';

Also Read : Shutdown system using PHP script.

Print numbers from 1 to N without using loop in PHP.

Import data from text file to MySQL using PHP.

Check PHP Script execution time measure.

Create a dynamic Calendar Using PHP.