• 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
6 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


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

  • Replace “\n” with new line characters, using Notepad++ 38 views
  • How to enter new line in Microsoft Teams? 34 views
  • Simple PHP Shopping Cart 18 views
  • Add FTP/SFTP in Visual Studio Code 15 views
  • Open a URL in a new tab using JavaScript 13 views
  • Solution: windows photo viewer opens each time save a pic file 12 views
  • Laravel: Download files to storage from SFTP 12 views
  • A Simple YouTube Video Downloader Script in PHP 11 views
  • Laravel Eloquent Select Column as Alias 10 views
  • Get Uploads Directory Path in WordPress 9 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