Google trends Public Holidays Coupon Code Code Compiler

Import MySQL Database Using PHP


Dec 25, 2018

Import MySQL Database Using PHP

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";

Copyright 2024. All rights are reserved