PHP script to import mysql database

You can import or export your database using phpMyAdmin provided by most of the server provider, Buy for some reason if you miss the control panel info then using the following script you can import your database. You only need the ftp info and database username, password, host name to connect the database.
Click here to know how to take backup of a database using PHP.

PHP Code


<?php
include("connection.php");

$filename = 'DB_Backup/filename.sql';

$templine = '';
$lines = file($filename); // Read entire file
foreach ($lines as $line){

if (substr($line, 0, 2) == '--' || $line == '') // Skip all comments
$templine .= $line;
if (substr(trim($line), -1, 1) == ';'){
	mysql_query($templine) or print('Error: '.mysql_error() . '<br >');
$templine = '';
}
}

?>


connection.php File

<?php
  $host="localhost";
  $uname="YourUserName";
  $pass="YourPassword";
  $database = "YourDatabaseName"; 
$connection=mysql_connect($host,$uname,$pass); 
or die("Database Connection Failed");
$selectdb=mysql_select_db($database) or die("Database could not be selected"); 
$result=mysql_select_db($database)
or die("database cannot be selected <br>");
?>

 

Comments
comments powered by Disqus