使用php“mysqli”导入大型转储

时间:2017-05-24 07:14:22

标签: php mysql mysqli

我尝试使用PHP脚本在solution中导入mysql数据库中的大型SQL文件。我使用mysqli而不是mysql。但是出现了这个错误:

  

上一个错误:执行查询时出错'/ *!40101 SET   @OLD_CHARACTER_SET_CLIENT = @@ CHARACTER_SET_CLIENT * /;

我确定SQL文件很好,问题出现在我的脚本中:

// SQL import
// your config
$filename = 'auct_lots_full.sql';
$maxRuntime = 8; // less then your max script execution limit


$deadline = time()+$maxRuntime;
$progressFilename = $filename.'_filepointer'; // tmp file for progress
$errorFilename = $filename.'_error'; // tmp file for erro

$con = mysqli_connect("localhost", "xxxx", "xxxxxxxx", "asystem_db") OR die("Database selection failed: " . mysqli_error($con));
mysqli_select_db($con, "asystem_db") OR die("Database selection failed: " . mysqli_error($con));


($fp = fopen($filename, 'r')) OR die('failed to open file:'.$filename);

// check for previous error
if( file_exists($errorFilename) ){
    die('<pre> previous error: '.file_get_contents($errorFilename));
}

// activate automatic reload in browser
echo '<html><head> <meta http-equiv="refresh" content="'.($maxRuntime+2).'"><pre>';

// go to previous file position
$filePosition = 0;
if( file_exists($progressFilename) ){
    $filePosition = file_get_contents($progressFilename);
    fseek($fp, $filePosition);
}

$queryCount = 0;
$query = '';
while( $deadline>time() AND ($line=fgets($fp, 1024000)) ){
    if(substr($line,0,2)=='--' OR trim($line)=='' ){
        continue;
    }

    $query .= $line;
    if( substr(trim($query),-1)==';' ){
        if( !mysqli_query($con, $query) ){
            $error = 'Error performing query \'<strong>' . $query . '\': ' . mysqli_error($con);
            file_put_contents($errorFilename, $error."\n");
            exit;
        }
        $query = '';
        file_put_contents($progressFilename, ftell($fp)); // save the current file position for
        $queryCount++;
    }
}

if( feof($fp) ){
    echo 'dump successfully restored!';
}else{
    echo ftell($fp).'/'.filesize($filename).' '.(round(ftell($fp)/filesize($filename), 2)*100).'%'."\n";
    echo $queryCount.' queries processed! please reload or wait for automatic browser refresh!';
}

0 个答案:

没有答案