Import MySQL Database Using PHP

Import MySQL Database Using PHP

Dec 25, 2018 / PHP / MySQL /

One of the most important tasks you needs to do import MySQL database. I am going to show you how you can easily restore MYSQL database using PHP. below is the simple PHP method for importing MYSQL database without PHPMyAdmin.

PHP Code

 


// database connection
    $UserName      = "root";
    $Password      = "";
    $HostName      = "localhost";
    $Db            = "test"
    
     
    $c mysqli($HostName, $UserName, $Password, $Db);
  
     
    $getFileLines = file('myfolder/database.sql');
  
    $error = '';
  
    // Loop through each line
    foreach ($getFileLines as $line){
        // Skip comment lines
        if(substr($line, 0, 2) == '--' || $line == ''){
            continue;
        }
  
        // Add this line to the variable
        $fileline .= $line;
  
        // If it has a semicolon at the end, it's the end of the query
        if (substr(trim($line), -1, 1) == ';'){
            // execute the query
            if(!$conn->query($fileline)){
                $error .= 'Error performing query ' . $fileline ;
            }
  
            // Reset variable
            $fileline= '';
        }
    }
    $conn->close();
    echo  !empty($error)?$error:"Database Import Success";

Comments

Rizwan ( Jan 26, 2019 09:39:28 )

Nice post, Thanks for sharing..

Would you like to share your thoughts?

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

Copyright 2018. All rights are reserved Significant Techno Web Design & Web Development By Significant Techno