- <?php
- $file_name = "d:test.sql";
- $dbhost = "localhost";
- $dbuser = "root";
- $dbpass = "";
- $dbname = "test";
- set_time_limit();
- $fp = @fopen($file_name,"r") or die("sql文件打不开");//打开文件
- $pdo = new PDO("mysql:host=localhost;dbname=test","root","");//连接数据库
- $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,)){
- $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)>){
- if($line[]=='-' && $line[]=="-"){
- continue;
- }
- }
- $sql .= $line.chr().chr();
- if(strlen($line)>){
- if($line[strlen($line)-]==";"){
- break;
- }
- }
- }
- return $sql;
- }
以上就是php+mysql将大数据sql文件导入数据库的详细内容,更多关于php+mysql将大数据sql文件导入数据库的资料请关注九品源码其它相关文章!