Home  >  Article  >  php教程  >  PHP实现导入大SQL文件

PHP实现导入大SQL文件

WBOY
WBOYOriginal
2016-06-06 20:11:361000browse

对于mysql的大量数据导入导出,直接在服务器上使用命令,会很方便快捷,但是有时候我们不能总结直接调用命令来实现,此时我们可以使用一个脚本语言作为调用这个命令的中介,这里我是用的是php里面的system函数。 system使用说明: string? system ?(?string?

对于mysql的大量数据导入导出,直接在服务器上使用命令,会很方便快捷,但是有时候我们不能总结直接调用命令来实现,此时我们可以使用一个脚本语言作为调用这个命令的中介,这里我是用的是php里面的system函数。

system使用说明:
string?system?(?string?$command?[,?int?&$return_var?] )

command: 执行的命令
return_var: 如果return_var存在,则为执行命令完后返回的结果
函数返回的结果为命令输出的最后一行。

举例:

<?php // Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system&#40;'ls', $retval&#41;;
 
// Printing additional info
echo 'Last line of the output: ' . $last_line;
echo 'Return value: ' . $retval;
?>

所以如果需要通过php导入大数据,可以使用:

<?php system&#40; "mysql -hDbhost -uDbuser -pPassword Dbname < backup.sql" &#41;; 
print  "导入成功" ; 
?>

Dbhost 改为您的数据库服务器地址
Dbuser 改为您的数据库用户名
Password 改为您的数据库用户密码
Dbname 改为您的数据库名
backup.sql 导入的sql文件

3.在浏览器里面访问mysql.php即可完成导入,同理如果需要导出sql语句也可以使用system命令来完成,其执行的时间相对比较快。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn