• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Tutsplanet

Tutsplanet

Free Technical and Blogging Resources

  • Home
  • Web Hosting
  • Programming
  • Plugins
  • Twitter Trends
  • Tools
  • About Us

Design & Development

How to import sql file in MySQL database using PHP?

Feb 18, 2018 Editorial Staff 2 Comments

Share
Tweet
Share
7 Shares

We usually use phpmyadmin for importing a large sql file to our database. There are tons of other options are there to do this. Most of the people use the phpMyAdmin tool for their database maintenance operations. But most of the host you have to login to cpanel and then navigate to the phpMyAdmin from there.

In this tutorial we are demonstrating how to make our own application that import  a .sql file using PHP script. This little php script helps you to restore our MySQL database with very few steps.

How to import sql file in MySQL database using PHP?

The following details needed in hand to perform the import action using PHP.

  • $filename – Required. The filename of dumped database.
  • $mysqlHost– Required. The mysql Host name
  • $mysqlUser – Required. The mysql server username.
  • $mysqlPassword – Required. The MySql user password. Often this can be empty for your localhost mysql installaiton. But it’s a must when you go in the production server.
  • $mysqlDatabase – The selected database that you need to import data.
<?php
// Name of the data file
$filename = 'mydump.sql';
// MySQL host
$mysqlHost = 'localhost';
// MySQL username
$mysqlUser = 'root';
// MySQL password
$mysqlPassword = '';
// Database name
$mysqlDatabase = 'newdatabase';

// Connect to MySQL server
$link = mysqli_connect($mysqlHost, $mysqlUser, $mysqlPassword, $mysqlDatabase) or die('Error connecting to MySQL Database: ' . mysqli_error());


$tempLine = '';
// Read in the full file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line) {

    // Skip it if it's a comment
    if (substr($line, 0, 2) == '--' || $line == '')
        continue;

    // Add this line to the current segment
    $tempLine .= $line;
    // If its semicolon at the end, so that is the end of one query
    if (substr(trim($line), -1, 1) == ';')  {
        // Perform the query
        mysqli_query($link, $tempLine) or print("Error in " . $tempLine .":". mysqli_error());
        // Reset temp variable to empty
        $tempLine = '';
    }
}
 echo "Tables imported successfully";
?>

You may be find this post useful, Create Simple Login Script in PHP and MySql

Did this post help you?
Tutsplanet brings in-depth and easy tutorials to understand even for beginners. This takes a considerable amount of work. If this post helps you, please consider supporting us as a token of appreciation:
  • Just want to thank us? Buy us a Coffee
  • May be another day? Shop on Amazon using our links.
    Your prices won't change but we get a small commission.

Editorial Staff

Editorial Staff at Tutsplanet is a dedicated team to write various tutorials about subjects like Programming, Technology and Operating Systems.

View all posts by Editorial Staff

Reader Interactions

Comments

  1. D'hoedt says

    Mar 21, 2021 at 3:35 pm

    the line
    mysqli_query($tempLine) or print(“Error in ” . $tempLine .”:”. mysqli_error());
    must be
    mysqli_query($link, $tempLine) or print(“Error in ” . $tempLine .”:”. mysqli_error());
    then it works well

    Reply
    • Editorial Staff says

      Mar 21, 2021 at 5:56 pm

      Yes You are Right. Thanks for pointing this. Updated the script.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Quick Links

  • Top 21 Website Ideas To Make Money Online in 2021
  • A Simple YouTube Video Downloader Script in PHP
  • The 50 Most Useful jQuery Plugins for Frontend Development
  • Replace “\n” with new line characters, using Notepad++
  • Using Third-Party Libraries in Codeigniter
  • Upload Multiple Images and Store in Database using PHP and MySQL.
  • Hierarchical Tree view Category Example in Laravel
  • Laravel Image Intervention Tutorial With Example
  • How to import sql file in MySQL database using PHP?
  • Free VAT Calculator Online

Subscribe

* indicates required

Search Here

Share

   

Hot topics

  • How Get Current URL from a PHTML – Magento 2 7 views
  • Replace “\n” with new line characters, using Notepad++ 5 views
  • How to enter new line in Microsoft Teams? 4 views
  • All Countries SQL file with Country Codes to Download 4 views
  • PHP: Implode () with quotes 3 views
  • Auto Login in PuTTY with a password 2 views
  • How to create Your Own Custom Log File in Laravel 2 views
  • Add FTP/SFTP in Visual Studio Code 2 views
  • How to clear laravel.log in Laravel? 2 views
  • Calendarize.js – Simplest JavaScript Calendar 2 views

Categories

  • Design & Development
  • Drupal
  • Facebook
  • General
  • How To
  • ios
  • Javascript
  • Linux
  • Magento
  • Marketing
  • News
  • PHP
  • Plugins
  • Programming
  • Snippets List
  • Social Media
  • Softwares
  • Themes
  • Tips
  • Wordpress
  • YouTube

Copyright © 2023 · Planet on Genesis Framework · Powered By BunnyCDN . Network wallpapernoon.com