Home >Backend Development >PHP Tutorial >Implementation code for php to import sql exported by phpmyadmin

Implementation code for php to import sql exported by phpmyadmin

WBOY
WBOYOriginal
2016-07-25 09:00:271457browse
The sql exported by phpmyadmin is handed over to the php program and imported into other libraries. How to achieve this? This article provides a good method for everyone. Friends in need can take a look.

The complete code is as follows.

<?php
//导入phpmyadmin导出的sql数据
//整理 bbs.it-home.org
//$file表示通过读取sql文件返回的字符串,比如$file2=file_get_contents('db.sql');

function import($file2){
$Db = new Db();
$file2=explode("\n",$file2);//将文件内容按行读入到数组

$c1=count($file2);
for($j=0;$j<$c1;$j++)
{
$ck=substr($file2[$j],0,4);//取每行的前4个字符
if( ereg("#",$ck)||ereg("--",$ck) )//去掉注释
{
continue;
}
$arr[]=$file2[$j];//将去掉注释的文件内容按行读入数组$arr,数组每个元素对应一行
}
$read=implode("\n",$arr); //重新组织文件内容到一个字符串,(按照原来分好的一行一行的)
$sql=str_replace("\r",'',$read);//去掉"\r(回车符)"

$detail=explode(";\n",$sql);
//将经上述整理过的文件内容再次按一条完整的sql语句(以;和\n分隔)导入到数组$detail,
//此时数组detail的每个元素对应一条完整的sql语句
$count=count($detail);
for($i=0;$i<$count;$i++)
{
$sql=str_replace("\r",'',$detail[$i]);//去掉每行sql中的回车符
$sql=str_replace("\n",'',$sql);//去掉换行符
$sql=trim($sql);//去掉前后空格
//现在的$sql
$Db->query($sql);
}
}
?>


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