发现自己之前写的php导出sql数据为Excel文件在导出一些数据的时候出现了精度的问题,比如导出身份证号的时候会把后面变成0000。暂时先把这个问题留下,有空去看看到底是什么问题。
写了一个导出sql的工具。稍微加了下界面,把功能也做多了点,简单来说,就是三个功能:
1.直接在浏览器上显示sql文件内容。
2.直接导出到服务器上,要求目录要可写。
3.直接下载到本地,脱库时感觉还不错。
好了直接贴上代码:
sqldump.php1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135<?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 "正在导出...<hr/>";
}
$info = "-- ----------------------------rn";
$info .= "-- 备份日期:".date("Y-m-d H:i:s",time())."rn";
$info .= "-- ----------------------------rnrn";
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 = "-- ----------------------------rn";
$info .= "-- Table structure for `".$val."`rn";
$info .= "-- ----------------------------rn";
$info .= "DROP TABLE IF EXISTS `".$val."`;rn";
$sqlStr = $info.$row[1].";rnrn";
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."`...<br>";
}
$sql = "select * from ".$val;
$res = @mysql_query($sql,$link);
//如果表中没有数据,则继续下一张表
if(@mysql_num_rows($res)<1) continue;
//
$info = "-- ----------------------------rn";
$info .= "-- Records for `".$val."`rn";
$info .= "-- ----------------------------rn";
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 .= ");rn";
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,"rn",FILE_APPEND);
}else{
echo "rn";
}
}
if(DOWNLOAD==2){
echo "<hr/>导出成功。";
}
// 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.html1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81<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;i<inputs.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 dump</h1>
<h3 align="right">Author:[email protected]</h3>
<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="默认:<empty>"/></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。
下面是客户端预览效果:
# PHP sql dump
### Author:[email protected]