Home > Article > Backend Development > PHP import .sql file into mysql database_PHP tutorial
php import .sql file into mysql database
php tutorial import .sql file into mysql tutorial database tutorial
set_time_limit(0); //Set the timeout to 0, which means it will always be executed. When php is invalid in safe mode, it may cause the import to time out. At this time, it needs to be imported in stages
$db = new mysql($location['host'],$location['hostname'],$location['hostpass'],$location['table'],"utf8",$location['ztime']) ;
$fp = @fopen($sql, "r") or die("Cannot open sql file $sql");//Open file
while($sql=getnextsql()){
Mysql_query($sql);
}
//echo "User data import completed!";
fclose($fp) or die("can't close file $file_name");//Close the file//Get sql from the file one by one
function getnextsql() {
global $fp;
$sql="";
While ($line = @fgets($fp, 40960)) {
$line = trim($line);
//The following three sentences are not needed in higher versions of php, but may need to be modified in some lower versions
//$line = str_replace("\","",$line);
//$line = str_replace("'","'",$line);
//$line = str_replace("rn",chr(13).chr(10),$line);
//$line = stripcslashes($line);
If (strlen($line)>1) {
If ($line[0]=="-" && $line[1]=="-") {
continue;
}
}
$sql.=$line.chr(13).chr(10);
If (strlen($line)>0){
If ($line[strlen($line)-1]==";"){
break;
}
}
}
Return $sql;
}
?>