Import of big database
Posted by: fritz müller
Date: December 06, 2012 03:00PM

if the import function of phpmyadmin fails, my script imports very big database dumps. Just use the script.


<?php
/*
Importscript to import Big SQL scripts
It handles very Big SQL scripts.

ATTENTION: JUST ONE SQL ORDER PER LINE (;)


1. Upload this script to your server. Change the SQL Properties before

2. Upload the SQL file to the same Folder (Best with FTP)

3. Call the Script with the SQL Filename as GET Parameter (Example: www.yourdomain.com/importskript.php?filename=sql.sql)

Copyright by Lucius Bachmann
*/
function db()//Function to Connect to Database
{
$MySQL_Host="Your-MySQL Host";
$MySQL_User="Your Mysql User";
$MySQL_Passw="Your Mysql Password";
$db="Your DB Name";


$dblk=mysql_connect("$MySQL_Host","$MySQL_User","$MySQL_Passw");
echo mysql_error($dblk);
mysql_select_db($db);
return($dblk);
}

$dblk=db();//Database connect
session_start();//start session
$maxtime=ini_get("max_execution_time");// get max execution time
$starttime=time();//starttime

if(isset($_SESSION['pointer'])){//If the skript was called by itself, set the variables as they where before
$pointer = $_SESSION['pointer'];
$sql = $_SESSION['sql'];
$counter = $_SESSION['counter'];
$fehler = $_SESSION['fehler'];
$Filename= $_SESSION['filename'];
$FileHandler = fopen($Filename, 'r') or die("can't open file");
fseek($FileHandler, $pointer);//Jump to the place the pointer was before
}
else{//If its the first call, initialize the Variables

$sql='';
$letter='';
$Filename = $_GET['filename'];

$pointer=0;

$counter=0;
$lastletters='';
$fehler='';
$_SESSION['skript_startet']=$starttime;
$FileHandler = fopen($Filename, 'r') or die("can't open file");
}

while(time()-$starttime+5 < $maxtime){// As long as you have Execution Time, And to avoid maximum execution timeout
$line=fgets($FileHandler);//Read line
if($line === false){//If End Of file, exit
$EndofFile=1;
break;
}
while(!(strpos($line, "--")===false)){//If a comment line, get next line
$line=fgets($FileHandler);

}
$pointer=ftell($FileHandler);//get the offset of the filecursor
$string=substr($line,0, strlen($line)-1);//read the line into a string
$sql.=$string;//append to sql order
$lastletter=substr($line, strlen($line)-2, strlen($line)-1);// get the last letter of the line
if(!(strpos($lastletter, ";")===false)){//If the last letter was a semicolon,

if(!mysql_query($sql)){//Mysql query
$fehler.="SQL Fehler: $sql <br>";// Fehler Save
}
$counter++;// count the orders
$sql='';
}

}

if(isset($EndofFile)){//If the sql import is finished, show stats
$timeUsed=time()-$_SESSION['skript_startet'];
echo"
SQL Befehle ausgeführt: $counter <br>
Time Used: $timeUsed seconds <br>
Fehler: $fehler <br>
";
}
else{//save the variables in the session

$_SESSION['pointer']=$pointer;
$_SESSION['sql']=$sql;
$_SESSION['counter']=$counter;
$_SESSION['fehler']=$fehler;
$_SESSION['filename']=$Filename;

echo "<script>window.location='".$_SERVER['PHP_SELF']."';</script>";//and call the script itself
}





?>

Options: ReplyQuote


Subject
Written By
Posted
Import of big database
December 06, 2012 03:00PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.