Zend Framework框架中用PHP实现数据的导入导出

实现方法:在indexAction中解析index.phtml表单(即为导出表单页面),在outputAction中获取要导出的数据表名,数据库名称,以及导出后文件的名称。就是把创建表的语句,和原先数据库表的数据组织起来写到一个自定义的sql文件中去。在upAction中解析up.phtml表单(即为导入表单页面),在uploadAction获取到要导入的文件路径和数据库名,分别组织创建表,和插入表语句,完成导入导出。具体代码如下:
双击代码全选
1
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
public function indexAction()
{
    $this->render();
}
public function outloadAction()
{
    $dbname=$this->_request->getPost('dbname');
    $tablename=$this->_request->getPost('tablename');
    $filename=$this->_request->getPost('filename');
    $host='localhost';
    $username='root';
    $password='root';
    $con=mysql_connect("localhost","root","root");
     if(!$con)
    {
       die('could not connect:'.mysql_error());
     }
    mysql_select_db($dbname,$con);
    $sql="describe ".$tablename;
    $rs=mysql_query($sql,$con);
    while($ss = mysql_fetch_row($rs))
    {
       $aa[0][]=$ss[0];
       $aa[1][]=$ss[1];
       $aa[2][]=$ss[2];
       $aa[3][]=$ss[3];
       $aa[4][]=$ss[4];
       $aa[5][]=$ss[5];
     }
    $createTable="create table ".$tablename."(";
     for($i=0;$i<count($aa[0]);$i++)
    {
       $createTable.="`".$aa[0][$i]."` ".$aa[1][$i]." ";
       if($aa[2][$i]=='NO')
      {
           $aa[2][$i]='NOT NULL';
       }
      $createTable.=$aa[2][$i]." ";
      if($aa[3][$i]=='PRI')
      {
           $aa[3][$i]="PRIMARY KEY (`".$aa[0][$i]."`)";
       }
    if($aa[3][$i]=='UNI')
      {
           $aa[3][$i]="UNIQUE KEY `".$aa[0][$i]."` (`".$aa[0][$i]."`)";
       }
    if($aa[3][$i]=='MUL')
      {
           $aa[3][$i]="KEY `".$aa[0][$i]."` USING BTREE (`".$aa[0][$i]."`)";
       }
    if($aa[4][$i]!="")
      {
           $aa[4][$i]="default `".$aa[4][$i]."` ";
       }
    $createTable.=$aa[4][$i].$aa[5][$i]." ,";   
    }
    for($i=0;$i<count($aa[0]);$i++)
    {
    if($aa[3][$i]!='')
      {
           $createTable.=$aa[3][$i].",";
      }
    }
    $createTable=rtrim($createTable,',');
    $createTable.=') ENGINE=InnoDB DEFAULT CHARSET=latin1;';
    $insertData="insert into ".$tablename."(";
    for($i=0;$i<count($aa[0]);$i++)
    {
     $insertData.="`".$aa[0][$i]."`,";
    }
    $insertData=rtrim($insertData,',');
    $insertData.=") VALUES ";
    $result=mysql_query('select * from '.$tablename);
    while($resObject = mysql_fetch_object($result))
    {
     $insertData.="(";
     for($i=0;$i<count($aa[0]);$i++)
      {
         if(is_numeric($resObject->$aa[0][$i]))
         {
            $insertData.=$resObject->$aa[0][$i].",";
          }
            else
         {
            $insertData.="'".$resObject->$aa[0][$i]."',";
          }
      }
     $insertData=rtrim($insertData,',');
     $insertData.="),";
    }
    $insertData=rtrim($insertData,',').";";
    //写入文件
    $filename=$filename.date('Y-m-d').".sql";
    $filePath=fopen("d:/".$filename,"at");
    fwrite($filePath,"\n\n".$createTable."\n\n");
    fwrite($filePath,$insertData);
    fclose($filePath);
}
 
public function upAction()
{
    $this->render();
}
public function uploadAction()
{
    $filename=$this->_request->getPost('filename');
    $dbname=$this->_request->getPost('dbname');
    $filePath=fopen($filename,'rt');
    $str1="";
    while(!feof($filePath))
    {
      $str=fgetc($filePath);
      $str1.=$str;
      if($str==';')
      {
        break;
       }
     }
    while (!feof($filePath))
    {
      $str2=fgets($filePath);   
    }
    fclose($filePath);
    $con=mysql_connect("localhost","root","root");
    if(!$con)
    {
      die('could not connect:'.mysql_error());
    }
    mysql_select_db($dbname,$con);
    $res=mysql_query($str1);
     $rs=mysql_query($str2);
    mysql_close();
 }
 
index.phtml
<?php header('Content-Type: text/html; charset=utf-8');?>
<html>
<head>
<title>outload</title>
</head>
<body>
<table width="260" border="1" bgcolor="#C4F7F0">
<form method="post" action="/load/outload">
<tr>
<td align="center" valign="middle">数据库名:</td>
<td><select name="dbname">
<?php
$con=mysql_connect("localhost","root","root");
if(!$con)
{
     die('could not connect:'.mysql_error());
 }
mysql_select_db("aaa",$con);
$sql='select SCHEMA_NAME from information_schema.SCHEMATA order by SCHEMA_NAME';
$res=mysql_query($sql);
while($rs=mysql_fetch_row($res))
{
  ?>
  <option value="<?php echo $rs[0]?>"><?php echo $rs[0]?></option>
  <?php
 }
?>
</select></td>
</tr>
<tr>
    <td align="center" valign="middle">表名:</td>
    <td><input type="text" name="tablename" size="20"></td>
</tr>
<tr>
    <td align="center" valign="middle">文件名:</td>
    <td><input type="text" name="filename" size="20"></td>
</tr>
<tr>
    <td align="center" valign="middle">&nbsp;</td>
    <td><input type="submit" name="submit" value="导出">&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" name="reset" value="重置"></td>
</tr>
</form>
</table>
</body>
</html>
 
 
up.phtml
<?php header('Content-Type: text/html; charset=utf-8');?>
<html>
<head>
<title>up</title>
</head>
<body>
<table width="260" border="1" bgcolor="#C4F7F0">
<form method="post" action="/load/upload">
<tr>
<td align="center" valign="middle">数据库名:</td>
<td align="center" valign="middle"><select name="dbname" >
<?php
$con=mysql_connect("localhost","root","root");
if(!$con)
{
  die('could not connect:'.mysql_error());
 }
mysql_select_db("aaa",$con);
$sql='select SCHEMA_NAME from information_schema.SCHEMATA order by SCHEMA_NAME';
$res=mysql_query($sql);
while($rs=mysql_fetch_row($res))
{
   ?>
  <option value="<?php echo $rs[0]?>"><?php echo $rs[0]?></option>
  <?php
 }
?>
</select></td>
</tr>
<tr>
    <td align="center" valign="middle">文件:</td>
    <td><input type="file" name="filename" ></td>
</tr>
<tr>
    <td align="center" valign="middle">&nbsp;</td>
    <td align="center" valign="middle"><input type="submit" name="submit" value="导出"></td>
</tr>
</form>
</table>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值