猿问

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

如何使用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:    test

MySQL User Name:    root

MySQL Password: NOTSHOWN

MySQL Host Name:    localhost

MySQL 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;

}

?>


梵蒂冈之花
浏览 1084回答 3
3回答

HUWWW

我还有另一种方法可以尝试<?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 servermysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());// Select databasemysql_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 lineforeach ($lines as $line){// Skip it if it's a commentif (substr($line, 0, 2) == '--' || $line == '')&nbsp; &nbsp; continue;// Add this line to the current segment$templine .= $line;// If it has a semicolon at the end, it's the end of the queryif (substr(trim($line), -1, 1) == ';'){&nbsp; &nbsp; // Perform the query&nbsp; &nbsp; mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');&nbsp; &nbsp; // Reset temp variable to empty&nbsp; &nbsp; $templine = '';}}&nbsp;echo "Tables imported successfully";?>这对我有用,祝你好运

慕沐林林

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;&nbsp;$progressFilename = $filename.'_filepointer'; // tmp file for progress$errorFilename = $filename.'_error'; // tmp file for erromysql_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 errorif( file_exists($errorFilename) ){&nbsp; &nbsp; die('<pre> previous error: '.file_get_contents($errorFilename));}// activate automatic reload in browserecho '<html><head> <meta http-equiv="refresh" content="'.($maxRuntime+2).'"><pre>';// go to previous file position$filePosition = 0;if( file_exists($progressFilename) ){&nbsp; &nbsp; $filePosition = file_get_contents($progressFilename);&nbsp; &nbsp; fseek($fp, $filePosition);}$queryCount = 0;$query = '';while( $deadline>time() AND ($line=fgets($fp, 1024000)) ){&nbsp; &nbsp; if(substr($line,0,2)=='--' OR trim($line)=='' ){&nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; }&nbsp; &nbsp; $query .= $line;&nbsp; &nbsp; if( substr(trim($query),-1)==';' ){&nbsp; &nbsp; &nbsp; &nbsp; if( !mysql_query($query) ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $error = 'Error performing query \'<strong>' . $query . '\': ' . mysql_error();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; file_put_contents($errorFilename, $error."\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $query = '';&nbsp; &nbsp; &nbsp; &nbsp; file_put_contents($progressFilename, ftell($fp)); // save the current file position for&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $queryCount++;&nbsp; &nbsp; }}if( feof($fp) ){&nbsp; &nbsp; echo 'dump successfully restored!';}else{&nbsp; &nbsp; echo ftell($fp).'/'.filesize($filename).' '.(round(ftell($fp)/filesize($filename), 2)*100).'%'."\n";&nbsp; &nbsp; echo $queryCount.' queries processed! please reload or wait for automatic browser refresh!';

慕哥6287543

您可以使用mysqli multi_query函数,如下所示:$sql = file_get_contents('mysqldump.sql');$mysqli = new mysqli("localhost", "root", "pass", "testdb");if (mysqli_connect_errno()) { /* check connection */&nbsp; &nbsp; printf("Connect failed: %s\n", mysqli_connect_error());&nbsp; &nbsp; exit();}/* execute multi query */if ($mysqli->multi_query($sql)) {&nbsp; &nbsp; echo "success";} else {&nbsp; &nbsp;echo "error";}
随时随地看视频慕课网APP
我要回答