php+mysql将大数据sql文件导入数据库
<?php $file_name = "d:test.sql"; $dbhost = "localhost"; $dbuser = "root"; $dbpass = "123456"; $dbname = "test"; set_time_limit(0); $fp = @fopen($file_name,"r") or die("sql文件打不开");//打开文件 $pdo = new PDO("mysql:host=localhost;dbname=test","root","123456");//连接数据库 $pdo->query('set names utf8');//设置编码 echo "正在执行导入操作"; while($SQL = GetNextSQL()){ if(!$pdo->query($SQL)){ echo "执行出错"; echo "SQL语句为".$SQL; } } echo "导入完成"; fclose($fp) or die("can't close file");//关闭文件 mysql_close(); //从文件中逐条取sql function GetNextSQL(){ global $fp; $sql=""; while($line = @fgets($fp,40960)){ $line = trim($line); $line = str_replace("////", "//", $line); $line = str_replace("/","'",$line); $line = str_replace("//r//n","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; }