Home > php教程 > php手册 > body text

php中比较简单的导入phpmyadmin生成的sql文件的方法

WBOY
Release: 2016-06-06 20:37:46
Original
1044 people have browsed it

做网站的时候 我们会制作一个安装文件 就需要用到sql文件创建数据库。分享一下 我所用的方法。

我们知道很多在网上下载的论坛或者个人博客程序都有安装的页面,要制作这样的安装程序就需要用到sql文件创建数据库。

只对phpmyadmin导出的sql文件有效
代码如下:
$dbfile="test.sql";
$content=iconv("UTF-8","GB2312",file_get_contents($dbfile));
//获取创建的数据
//去掉注释
$content=preg_replace("/--.*\n/iU","",$content);
//替换前缀
$content=str_replace("ct_",TABLE_PRE,$content);

$carr=array();
$iarr=array();
//提取create
preg_match_all("/Create table .*\(.*\).*\;/iUs",$content,$carr);
$carr=$carr[0];
foreach($carr as $c)
{
@mysql_query($c,$link);
}

//提取insert
preg_match_all("/INSERT INTO .*\(.*\)\;/iUs",$content,$iarr);
$iarr=$iarr[0];
//插入数据
foreach($iarr as $c)
{
@mysql_query($c,$link);
}
Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template