Home  >  Article  >  Backend Development  >  PHP import .sql file into mysql database_PHP tutorial

PHP import .sql file into mysql database_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:05:00991browse

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;
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630813.htmlTechArticlephp import .sql file to mysql database php tutorial import .sql file to mysql tutorial database tutorial set_time_limit(0); //Set the timeout to 0, which means it will always be executed. When php is in safe mode...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn