1. {
  2. var $conn;
  3. function Db($host="localhost",$user="root",$pass="root",$db="test")
  4. {
  5. if(!$this->conn=mysql_connect($host,$user,$pass))
  6. die("can't connect to mysql sever");
  7. mysql_select_db($db,$this->conn);
  8. mysql_query("SET NAMES 'UTF-8'");
  9. }
  10. function execute($sql)
  11. {
  12. return mysql_query($sql,$this->conn);
  13. }
  14. function findCount($sql)
  15. {
  16. $result=$this->execute($sql);
  17. return mysql_num_rows($result);
  18. }
  19. function findBySql($sql)
  20. {
  21. $array=array();
  22. $result=mysql_query($sql);
  23. $i=0;
  24. while($row=mysql_fetch_assoc($result))
  25. {
  26. $array[$i]=$row;
  27. $i++;
  28. }
  29. return $array;
  30. }
  31. //$con的几种情况
  32. //空:返回全部记录
  33. //array:eg. array('id'=>'1') 返回id=1的记录
  34. //string :eg. 'id=1' 返回id=1的记录
  35. function toExtJson($table,$start="0",$limit="10",$cons="")
  36. {
  37. $sql=$this->generateSql($table,$cons);
  38. $totalNum=$this->findCount($sql);
  39. $result=$this->findBySql($sql." LIMIT ".$start." ,".$limit);
  40. $resultNum = count($result);//当前结果数
  41. $str="";
  42. $str.= "{";
  43. $str.= "'totalCount':'$totalNum',";
  44. $str.="'rows':";
  45. $str.="[";
  46. for($i=0;$i<$resultNum;$i++){
  47. $str.="{";
  48. $count=count($result[$i]);
  49. $j=1;
  50. foreach($result[$i] as $key=>$val)
  51. {
  52. if($j<$count)
  53. {
  54. $str.="'".$key."':'".$val."',";
  55. }
  56. elseif($j==$count)
  57. {
  58. $str.="'".$key."':'".$val."'";
  59. }
  60. $j++;
  61. }
  62. $str.="}";
  63. if ($i != $resultNum-1) {
  64. $str.= ",";
  65. }
  66. }
  67. $str.="]";
  68. $str.="}";
  69. return $str;
  70. }
  71. function generateSql($table,$cons)
  72. {
  73. $sql="";//sql条件
  74. $sql="select * from ".$table;
  75. if($cons!="")
  76. {
  77. if(is_array($cons))
  78. {
  79. $k=0;
  80. foreach($cons as $key=>$val)
  81. {
  82. if($k==0)
  83. {
  84. $sql.="where '";
  85. $sql.=$key;
  86. $sql.="'='";
  87. $sql.=$val."'";
  88. }else
  89. {
  90. $sql.="and '";
  91. $sql.=$key;
  92. $sql.="'='";
  93. $sql.=$val."'";
  94. }
  95. $k++;
  96. }
  97. }else
  98. {
  99. $sql.=" where ".$cons;
  100. }
  101. }
  102. return $sql;
  103. }
  104. function toExt\n";
  105. $\n";
  106. return $ }
  107. //输出word表格
  108. function toWord($table,$mapping,$fileName)
  109. {
  110. header('Content-type: application/doc');
  111. header('Content-Disposition: attachment; filename="'.$fileName.'.doc"');
  112. echo '';
  113. }
  114. function toExcel($table,$mapping,$fileName)
  115. {
  116. header("Content-type:application/vnd.ms-excel");
  117. header("Content-Disposition:filename=".$fileName.".xls");
  118. echo'';
  119. }
  120. function Backup($table)
  121. {
  122. if(is_array ($table))
  123. {
  124. $str="";
  125. foreach($table as $tab)
  126. $str.=$this->get_table_content($tab);
  127. return $str;
  128. }else{
  129. return $this->get_table_content($table);
  130. }
  131. }
  132. function Backuptofile($table,$file)
  133. {
  134. header("Content-disposition: filename=$file.sql");//所保存的文件名
  135. header("Content-type: application/octetstream");
  136. header("Pragma: no-cache");
  137. header("Expires: 0");
  138. if(is_array ($table))
  139. {
  140. $str="";
  141. foreach($table as $tab)
  142. $str.=$this->get_table_content($tab);
  143. echo $str;
  144. }else{
  145. echo $this->get_table_content($table);
  146. }
  147. }
  148. function Restore($table,$file="",$content="")
  149. {
  150. //排除file,content都为空或者都不为空的情况
  151. if(($file==""&&$content=="")||($file!=""&&$content!=""))
  152. echo"参数错误";
  153. $this->truncate($table);
  154. if($file!="")
  155. {
  156. if($this->RestoreFromFile($file))
  157. return true;
  158. else
  159. return false;
  160. }
  161. if($content!="")
  162. {
  163. if($this->RestoreFromContent($content))
  164. return true;
  165. else
  166. return false;
  167. }
  168. }
  169. //清空表,以便恢复数据
  170. function truncate($table)
  171. {
  172. if(is_array ($table))
  173. {
  174. $str="";
  175. foreach($table as $tab)
  176. $this->execute("TRUNCATE TABLE $tab");
  177. }else{
  178. $this->execute("TRUNCATE TABLE $table");
  179. }
  180. }
  181. function get_table_content($table)
  182. {
  183. $results=$this->findBySql("select * from $table");
  184. $temp = "";
  185. $crlf="
    ";
  186. foreach($results as $result)
  187. {
  188. /*(";
  189. foreach($result as $key=>$val)
  190. {
  191. $schema_insert .= " `".$key."`,";
  192. }
  193. $schema_insert = ereg_replace(",$", "", $schema_insert);
  194. $schema_insert .= ")
  195. */
  196. $schema_insert = "INSERT INTO $table VALUES (";
  197. foreach($result as $key=>$val)
  198. {
  199. if($val != "")
  200. $schema_insert .= " '".addslashes($val)."',";
  201. else
  202. $schema_insert .= "NULL,";
  203. }
  204. $schema_insert = ereg_replace(",$", "", $schema_insert);
  205. $schema_insert .= ");$crlf";
  206. $temp = $temp.$schema_insert ;
  207. }
  208. return $temp;
  209. }
  210. function RestoreFromFile($file){
  211. if (false !== ($fp = fopen($file, 'r'))) {
  212. $sql_queries = trim(fread($fp, filesize($file)));
  213. $this->splitMySqlFile($pieces, $sql_queries);
  214. foreach ($pieces as $query) {
  215. if(!$this->execute(trim($query)))
  216. return false;
  217. }
  218. return true;
  219. }
  220. return false;
  221. }
  222. function RestoreFromContent($content)
  223. {
  224. $content = trim($content);
  225. $this->splitMySqlFile($pieces, $content);
  226. foreach ($pieces as $query) {
  227. if(!$this->execute(trim($query)))
  228. return false;
  229. }
  230. return true;
  231. }
  232. function splitMySqlFile(&$ret, $sql)
  233. {
  234. $sql= trim($sql);
  235. $sql=split(';',$sql);
  236. $arr=array();
  237. foreach($sql as $sq)
  238. {
  239. if($sq!="");
  240. $arr[]=$sq;
  241. }
  242. $ret=$arr;
  243. return true;
  244. }
  245. }
  246. $db=new db();
  247. // 生成 word
  248. //$map=array('No','Name','Email','Age');
  249. //echo $db->toWord('test',$map,'档案');
  250. // 生成 Excel
  251. //$map=array('No','Name','Email','Age');
  252. //echo $db->toExcel('test',$map,'档案');
  253. // 生成 //echo $db->toExt
  254. // 生成 Json
  255. //echo $db->toExtJson('test',0,20);
  256. //备份
  257. //echo $db->Backuptofile('test','backup');
  258. ?>
复制代码

2、整表备份 1 2 下一页 尾页



09-18 23:49