dc.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. //header('Content-type: text/html; charset=UTF-8');
  3. //header("Content-type:application/vnd.ms-excel;charset=UTF-8");
  4. //header("Content-Disposition:filename=example.xls"); //输出的表格名称
  5. //echo "id\t";echo "type\t";echo "name\t";echo "content\t";echo "url\t";echo "ord\t\n";
  6. ////这是表格头字段 加\T就是换格,加\T\N就是结束这一行,换行的意思
  7. //$conn = mysql_connect("localhost","a0313085524","1f84d01e") or die("不能连接数据库");
  8. //mysql_select_db("a0313085524", $conn);
  9. //mysql_query("set names 'UTF-8'");
  10. //$sql="select * from example";
  11. //$result=mysql_query($sql);
  12. //while($row=mysql_fetch_array($result)){
  13. // echo $row[0]."\t";echo $row[1]."\t";echo $row[2]."\t";echo "'".$row[3]."'\t";echo $row[4]."\t";echo $row[5]."\t\n";
  14. //}
  15. //header("Content-type:application/vnd.ms-excel");
  16. //header("Content-Disposition:filename=xls_region.xls");
  17. //
  18. //$cfg_dbhost = 'localhost';
  19. //$cfg_dbname = 'a0313085524';
  20. //$cfg_dbuser = 'a0313085524';
  21. //$cfg_dbpwd = '1f84d01e';
  22. //$cfg_db_language = 'utf8';
  23. //// END 配置
  24. ////链接数据库
  25. //$link = mysql_connect($cfg_dbhost, $cfg_dbuser, $cfg_dbpwd);
  26. //mysql_select_db($cfg_dbname);
  27. ////选择编码
  28. //mysql_query("set names " . $cfg_db_language);
  29. ////users表
  30. //$sql = "desc example";
  31. //
  32. //$res = mysql_query($sql);
  33. //echo "<table><tr>";
  34. ////导出表头(也就是表中拥有的字段)
  35. //while ($row = mysql_fetch_array($res)) {
  36. // $t_field[] = $row['Field'];//Field中的F要大写,否则没有结果
  37. // echo "<th>" . $row['Field'] . "</th>";
  38. //}
  39. //echo "</tr>";
  40. ////导出100条数据
  41. //$sql = "select * from example";
  42. //$res = mysql_query($sql);
  43. //while ($row = mysql_fetch_array($res)) {
  44. // echo "<tr>";
  45. // foreach ($t_field as $f_key) {
  46. // echo "<td>'" . $row[$f_key] . "'</td>";
  47. // }
  48. // echo "</tr>";
  49. //}
  50. //echo "</table>";
  51. header("Content-type:text/html;charset=utf-8");
  52. //配置信息
  53. $cfg_dbhost = 'localhost';
  54. $cfg_dbname = 'a0313085524';
  55. $cfg_dbuser = 'a0313085524';
  56. $cfg_dbpwd = '1f84d01e';
  57. $cfg_db_language = 'utf8';
  58. $to_file_name = "example.sql";
  59. // END 配置
  60. //链接数据库
  61. $link = mysql_connect($cfg_dbhost, $cfg_dbuser, $cfg_dbpwd);
  62. mysql_select_db($cfg_dbname);
  63. //选择编码
  64. mysql_query("set names " . $cfg_db_language);
  65. //数据库中有哪些表
  66. $tables = mysql_list_tables($cfg_dbname);
  67. //将这些表记录到一个数组
  68. $tabList = array();
  69. while($row = mysql_fetch_row($tables)){
  70. $tabList[] = $row[0];
  71. }
  72. echo "运行中,请耐心等待...<br/>";
  73. $info = "-- ----------------------------\r\n";
  74. $info .= "-- 日期:" . date("Y-m-d H:i:s", time()) . "\r\n";
  75. $info .= "-- 仅用于测试和学习,本程序不适合处理超大量数据\r\n";
  76. $info .= "-- ----------------------------\r\n\r\n";
  77. file_put_contents($to_file_name, $info, FILE_APPEND);
  78. //将每个表的表结构导出到文件
  79. foreach($tabList as $val){
  80. $sql = "show create table " . $val;
  81. $res = mysql_query($sql, $link);
  82. $row = mysql_fetch_array($res);
  83. $info = "-- ----------------------------\r\n";
  84. $info .= "-- Table structure for `" . $val . "`\r\n";
  85. $info .= "-- ----------------------------\r\n";
  86. $info .= "DROP TABLE IF EXISTS `" . $val . "`;\r\n";
  87. $sqlStr = $info . $row[1] . ";\r\n\r\n";
  88. //追加到文件
  89. file_put_contents($to_file_name, $sqlStr, FILE_APPEND);
  90. //释放资源
  91. mysql_free_result($res);
  92. }
  93. //将每个表的数据导出到文件
  94. foreach($tabList as $val){
  95. $sql = "select * from " . $val;
  96. $res = mysql_query($sql, $link);
  97. //如果表中没有数据,则继续下一张表
  98. if(mysql_num_rows($res) < 1) continue;
  99. //
  100. $info = "-- ----------------------------\r\n";
  101. $info .= "-- Records for `" . $val . "`\r\n";
  102. $info .= "-- ----------------------------\r\n";
  103. file_put_contents($to_file_name, $info, FILE_APPEND);
  104. //读取数据
  105. while($row = mysql_fetch_row($res)){
  106. $sqlStr = "INSERT INTO `" . $val . "` VALUES (";
  107. foreach($row as $zd){
  108. $sqlStr .= "'" . $zd . "', ";
  109. }
  110. //去掉最后一个逗号和空格
  111. $sqlStr = substr($sqlStr, 0, strlen($sqlStr) - 2);
  112. $sqlStr .= ");\r\n";
  113. file_put_contents($to_file_name, $sqlStr, FILE_APPEND);
  114. }
  115. //释放资源
  116. mysql_free_result($res);
  117. file_put_contents($to_file_name, "\r\n", FILE_APPEND);
  118. }
  119. echo "OK!";
  120. ?>