为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用php在mysql数据库中导入.sql文件

如何使用php在mysql数据库中导入.sql文件

梵蒂冈之花 2019-11-22 15:20:59
如何使用php在mysql数据库中导入.sql文件我该怎么办,请帮助我解决此问题,谢谢此代码显示此错误。There was an error during import. Please make sure the import file is saved in the same folder as this script and check your values:MySQL Database Name:    testMySQL User Name:    rootMySQL Password: NOTSHOWNMySQL Host Name:    localhostMySQL Import Filename:  dbbackupmember.sql我正在使用此代码<?php//ENTER THE RELEVANT INFO BELOW$mysqlDatabaseName ='test';$mysqlUserName ='root';$mysqlPassword ='';$mysqlHostName ='localhost';$mysqlImportFilename ='dbbackupmember.sql';//DONT EDIT BELOW THIS LINE//Export the database and output the status to the page$command='mysql -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p' .$mysqlPassword .' ' .$mysqlDatabaseName .' < ' .$mysqlImportFilename;exec($command,$output=array(),$worked);switch($worked){    case 0:        echo 'Import file <b>' .$mysqlImportFilename .'</b> successfully imported to database <b>' .$mysqlDatabaseName .'</b>';        break;    case 1:        echo 'There was an error during import. Please make sure the import file is saved in the same folder as this script and check your values:<br/><br/><table><tr><td>MySQL Database Name:</td><td><b>' .$mysqlDatabaseName .'</b></td></tr><tr><td>MySQL User Name:</td><td><b>' .$mysqlUserName .'</b></td></tr><tr><td>MySQL Password:</td><td><b>NOTSHOWN</b></td></tr><tr><td>MySQL Host Name:</td><td><b>' .$mysqlHostName .'</b></td></tr><tr><td>MySQL Import Filename:</td><td><b>' .$mysqlImportFilename .'</b></td></tr></table>';        break;}?>
查看完整描述

3 回答

?
HUWWW

TA贡献1874条经验 获得超12个赞

我还有另一种方法可以尝试


<?php


// Name of the file

$filename = 'churc.sql';

// MySQL host

$mysql_host = 'localhost';

// MySQL username

$mysql_username = 'root';

// MySQL password

$mysql_password = '';

// Database name

$mysql_database = 'dump';


// Connect to MySQL server

mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());

// Select database

mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());


// Temporary variable, used to store current query

$templine = '';

// Read in entire file

$lines = file($filename);

// Loop through each line

foreach ($lines as $line)

{

// Skip it if it's a comment

if (substr($line, 0, 2) == '--' || $line == '')

    continue;


// Add this line to the current segment

$templine .= $line;

// If it has a semicolon at the end, it's the end of the query

if (substr(trim($line), -1, 1) == ';')

{

    // Perform the query

    mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');

    // Reset temp variable to empty

    $templine = '';

}

}

 echo "Tables imported successfully";

?>

这对我有用,祝你好运


查看完整回答
反对 回复 2019-11-22
?
慕沐林林

TA贡献2016条经验 获得超9个赞

Raj的答案很有用,但是(由于file($ filename))如果您的mysql-dump不适合内存,它将失败


如果您在共享主机上,并且有30 MB和12s脚本运行时之类的限制,并且必须还原x00MB mysql dump,则可以使用以下脚本:


它将在转储文件查询中进行查询,如果脚本执行截止日期临近,它将当前文件位置保存在tmp文件中,并且自动重新加载浏览器将一次又一次地继续此过程...如果发生错误,则重新加载将停止并显示错误...


如果您从午餐回来,您的数据库将恢复;-)


noLimitDumpRestore.php:


// your config

$filename = 'yourGigaByteDump.sql';

$dbHost = 'localhost';

$dbUser = 'user';

$dbPass = '__pass__';

$dbName = 'dbname';

$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


mysql_connect($dbHost, $dbUser, $dbPass) OR die('connecting to host: '.$dbHost.' failed: '.mysql_error());

mysql_select_db($dbName) OR die('select db: '.$dbName.' failed: '.mysql_error());


($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( !mysql_query($query) ){

            $error = 'Error performing query \'<strong>' . $query . '\': ' . mysql_error();

            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!';


查看完整回答
反对 回复 2019-11-22
?
慕哥6287543

TA贡献1831条经验 获得超10个赞

您可以使用mysqli multi_query函数,如下所示:


$sql = file_get_contents('mysqldump.sql');


$mysqli = new mysqli("localhost", "root", "pass", "testdb");

if (mysqli_connect_errno()) { /* check connection */

    printf("Connect failed: %s\n", mysqli_connect_error());

    exit();

}


/* execute multi query */

if ($mysqli->multi_query($sql)) {

    echo "success";

} else {

   echo "error";

}


查看完整回答
反对 回复 2019-11-22
  • 3 回答
  • 0 关注
  • 1073 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信