您好,欢迎访问一九零五行业门户网

PHP导出sql文件

发现自己之前写的php导出sql数据为excel文件在导出一些数据的时候出现了精度的问题,比如导出身份证号的时候会把后面变成0000。暂时先把这个问题留下,有空去看看到底是什么问题。 写了一个导出sql的工具。稍微加了下界面,把功能也做多了点,简单来说,就是
发现自己之前写的php导出sql数据为excel文件在导出一些数据的时候出现了精度的问题,比如导出身份证号的时候会把后面变成0000。暂时先把这个问题留下,有空去看看到底是什么问题。
写了一个导出sql的工具。稍微加了下界面,把功能也做多了点,简单来说,就是三个功能:
1.直接在浏览器上显示sql文件内容。
2.直接导出到服务器上,要求目录要可写。
3.直接下载到本地,脱库时感觉还不错。
好了直接贴上代码:
 sqldump.php
php if (isset($_request['option'])) { define('download', $_request['option']); }else{ define('download',0);//0代表直接显示,1代表下载,2代表导出在本地 } if (download != 1) { header(content-type:text/html;charset=utf-8); } $cfg_dbhost ='localhost'; $cfg_dbname ='mysql'; $cfg_dbuser ='root'; $cfg_dbpwd =''; $cfg_db_language ='utf8'; //配置信息 $cfg_dbhost = isset($_request['dbhost'])?$_request['dbhost']:$cfg_dbhost; $cfg_dbname = isset($_request['dbname'])?$_request['dbname']:$cfg_dbname; $cfg_dbuser = isset($_request['dbuser'])?$_request['dbuser']:$cfg_dbuser; $cfg_dbpwd = isset($_request['dbpwd'])?$_request['dbpwd']:$cfg_dbpwd; $cfg_db_language = isset($_request['dbc'])?$_request['dbc']:$cfg_db_language; $to_file_name =isset($_request['dbtable'])?$_request['dbtable']..sql:$cfg_dbname..sql; if (download==2) { $to_file_name =isset($_request['dbtable'])?dirname(__file__).directory_separator.$_request['dbtable']..sql:dirname(__file__).directory_separator.$cfg_dbname..sql; } // end 配置 //链接数据库 $link = @mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd); $link==null?die('mysql connect error'):''; @mysql_select_db($cfg_dbname); //选择编码 @mysql_query(set names .$cfg_db_language); //数据库中有哪些表 $tablist = isset($_request['dbtable'])?array({$_request['dbtable']}):list_tables($cfg_dbname); $tablist==null?die('no tables found'):''; if (download==1) { header(content-type: application/octet-stream); header(accept-ranges: bytes); header(content-disposition: attachment; filename=.$to_file_name); } if (download==2) { echo 正在导出...; } $info = -- ----------------------------\r\n; $info .= -- 备份日期:.date(y-m-d h:i:s,time()).\r\n; $info .= -- ----------------------------\r\n\r\n; if (download==2) { file_put_contents($to_file_name,$info,file_append); }else{ echo $info; } //将每个表的表结构导出到文件 foreach($tablist as $val){ $sql = show create table .$val; $res = @mysql_query($sql,$link); if ($res==null) { die('table `'.$val.'` not exists'); } $row = @mysql_fetch_array($res); $info = -- ----------------------------\r\n; $info .= -- table structure for `.$val.`\r\n; $info .= -- ----------------------------\r\n; $info .= drop table if exists `.$val.`;\r\n; $sqlstr = $info.$row[1].;\r\n\r\n; if (download==2) { //追加到文件 file_put_contents($to_file_name,$sqlstr,file_append); }else{ echo $sqlstr; } //释放资源 @mysql_free_result($res); } //将每个表的数据导出到文件 foreach($tablist as $val){ if(download==2){ echo 正在导出表`.$val.`...
; } $sql = select * from .$val; $res = @mysql_query($sql,$link); //如果表中没有数据,则继续下一张表 if(@mysql_num_rows($res)continue; // $info = -- ----------------------------\r\n; $info .= -- records for `.$val.`\r\n; $info .= -- ----------------------------\r\n; if (download==2) { file_put_contents($to_file_name,$info,file_append); }else{ echo $info; } //读取数据 while($row = @mysql_fetch_row($res)){ $sqlstr = insert into `.$val.` values (; foreach($row as $zd){ $sqlstr .= '.$zd.', ; } //去掉最后一个逗号和空格 $sqlstr = substr($sqlstr,0,strlen($sqlstr)-2); $sqlstr .= );\r\n; if (download==2) { file_put_contents($to_file_name,$sqlstr,file_append); }else{ echo $sqlstr; } } //释放资源 @mysql_free_result($res); if (download==2) { file_put_contents($to_file_name,\r\n,file_append); }else{ echo \r\n; } } if(download==2){ echo 导出成功。; }// echo end!; function list_tables($database) { $sql='show tables from '.$database; $rs = mysql_query($sql); $tables = array(); while ($row = mysql_fetch_row($rs)) { $tables[] = $row[0]; } mysql_free_result($rs); return $tables; }?>
然后是界面的东西了,为的是不让自己去记这个烦人的参数。
sqldumpclient.html
html>head>title>sqldump客户端title>meta charset='utf-8'>script type=text/javascript> function setaction () { $('cform').action=$('act').value; inputs=document.getelementsbytagname('input'); for(var i =1;iinputs.length-4;i++){ if (inputs[i].value!='') { inputs[i].name=inputs[i].id; } } } function $(id){ return document.getelementbyid(id); }script>head>body>center> h1 align=center>php sql dumph1>h3 align=right>author:medici.yan@gmail.comh3> form method=post id='cform' onsubmit=setaction()> fieldset> table> legend>配置legend> tr> td>label for=act>脚本地址:label>td> td>input type=text id='act' placeholder=http://www.example.com/sqldump.php/>td> td rowspan=7 width=100>使用方法:把sqldump.php传到服务器上,然后在这里填写相关参数,也可以直接访问脚本td> tr> tr> td>label for=dbhost>数据库地址:label>td> td>input type=text id=dbhost placeholder=默认:localhost/>td> tr> tr> td>label for=dbname>数据库名:label>td> td>input type=text id=dbname placeholder=默认:mysql/>td> tr> tr> td>label for=dbuser>用户名:label>td> td>input type=text id=dbuser placeholder=默认:root/>td> tr> tr> td>label for=dbpwd>密码:label>td> td>input type=text id=dbpwd placeholder=默认:/>td> tr> tr> td>label for=dbc>字符集:label>td> td> input list=charlist id=dbc placeholder=默认:utf8 /> datalist id=charlist> option value=utf8 /> option value=gbk /> option value=gb2312 /> datalist> td> tr> tr> td>label for=dbtable>表名:label>td> td>input type=text id=dbtable placeholder=为空代表所有表/>td> tr> tr> td>label for=option>导出方式:label>td> td> input type=radio id=option name=option value=0 checked=checked>浏览器显示 input type=radio id=option name=option value=1>下载到本地 input type=radio id=option name=option value=2>导出至服务器 td> tr> tr> td>input type=submit value=开始 />td> tr> table> fieldset> form>center>body>html>
用法也很简单,把sqldump.php传到服务器上,然后在本地运行sqldumpclient.html,写上相关参数就ok。
其它类似信息

推荐信息