php資料備份:單表備份 整表備份 匯入資料庫

來源:互聯網
上載者:User
  1. class Db
  2. {
  3. var $conn;
  4. function Db($host="localhost",$user="root",$pass="root",$db="test")
  5. {
  6. if(!$this->conn=mysql_connect($host,$user,$pass))
  7. die("can't connect to mysql sever");
  8. mysql_select_db($db,$this->conn);
  9. mysql_query("SET NAMES 'UTF-8'");
  10. }
  11. function execute($sql)
  12. {
  13. return mysql_query($sql,$this->conn);
  14. }
  15. function findCount($sql)
  16. {
  17. $result=$this->execute($sql);
  18. return mysql_num_rows($result);
  19. }
  20. function findBySql($sql)
  21. {
  22. $array=array();
  23. $result=mysql_query($sql);
  24. $i=0;
  25. while($row=mysql_fetch_assoc($result))
  26. {
  27. $array[$i]=$row;
  28. $i++;
  29. }
  30. return $array;
  31. }
  32. //$con的幾種情況
  33. //空:返回全部記錄
  34. //array:eg. array('id'=>'1') 返回id=1的記錄
  35. //string :eg. 'id=1' 返回id=1的記錄
  36. function toExtJson($table,$start="0",$limit="10",$cons="")
  37. {
  38. $sql=$this->generateSql($table,$cons);
  39. $totalNum=$this->findCount($sql);
  40. $result=$this->findBySql($sql." LIMIT ".$start." ,".$limit);
  41. $resultNum = count($result);//當前結果數
  42. $str="";
  43. $str.= "{";
  44. $str.= "'totalCount':'$totalNum',";
  45. $str.="'rows':";
  46. $str.="[";
  47. for($i=0;$i<$resultNum;$i++){
  48. $str.="{";
  49. $count=count($result[$i]);
  50. $j=1;
  51. foreach($result[$i] as $key=>$val)
  52. {
  53. if($j<$count)
  54. {
  55. $str.="'".$key."':'".$val."',";
  56. }
  57. elseif($j==$count)
  58. {
  59. $str.="'".$key."':'".$val."'";
  60. }
  61. $j++;
  62. }
  63. $str.="}";
  64. if ($i != $resultNum-1) {
  65. $str.= ",";
  66. }
  67. }
  68. $str.="]";
  69. $str.="}";
  70. return $str;
  71. }
  72. function generateSql($table,$cons)
  73. {
  74. $sql="";//sql條件
  75. $sql="select * from ".$table;
  76. if($cons!="")
  77. {
  78. if(is_array($cons))
  79. {
  80. $k=0;
  81. foreach($cons as $key=>$val)
  82. {
  83. if($k==0)
  84. {
  85. $sql.="where '";
  86. $sql.=$key;
  87. $sql.="'='";
  88. $sql.=$val."'";
  89. }else
  90. {
  91. $sql.="and '";
  92. $sql.=$key;
  93. $sql.="'='";
  94. $sql.=$val."'";
  95. }
  96. $k++;
  97. }
  98. }else
  99. {
  100. $sql.=" where ".$cons;
  101. }
  102. }
  103. return $sql;
  104. }
  105. function toExtXml($table,$start="0",$limit="10",$cons="")
  106. {
  107. $sql=$this->generateSql($table,$cons);
  108. $totalNum=$this->findCount($sql);
  109. $result=$this->findBySql($sql." LIMIT ".$start." ,".$limit);
  110. $resultNum = count($result);//當前結果數
  111. header("Content-Type: text/xml");
  112. $xml="\n";
  113. $xml.="\n";
  114. $xml.="\t".$totalNum."\n";
  115. $xml.="\t\n";
  116. for($i=0;$i<$resultNum;$i++){
  117. $xml.="\t\t\n";
  118. foreach($result[$i] as $key=>$val)
  119. $xml.="\t\t\t<".$key.">".$val."\n";
  120. $xml.="\t\t\n";
  121. }
  122. $xml.="\t\n";
  123. $xml.="\n";
  124. return $xml;
  125. }
  126. //輸出word表格
  127. function toWord($table,$mapping,$fileName)
  128. {
  129. header('Content-type: application/doc');
  130. header('Content-Disposition: attachment; filename="'.$fileName.'.doc"');
  131. echo ' xmlns:w="urn:schemas-microsoft-com:office:word"
  132. xmlns="http://www.w3.org/TR/REC-html40">
  133. '.$fileName.'
  134. ';
  135. echo'
  136. if(is_array($mapping))
  137. }
  138. $results=$this->findBySql('select * from '.$table);
  139. foreach($result as $key=>$val)
  140. echo'
  141. }
  142. ';
  143. {
  144. foreach($mapping as $key=>$val)
  145. echo'
  146. ';
  147. echo'
  148. ';
  149. foreach($results as $result)
  150. {
  151. echo'
  152. ';
  153. echo'
  154. '; ';
  155. echo'
  156. '.$val.'
    '.$val.'
    ';
  157. echo'';
  158. echo'';
  159. }
  160. function toExcel($table,$mapping,$fileName)
  161. {
  162. header("Content-type:application/vnd.ms-excel");
  163. header("Content-Disposition:filename=".$fileName.".xls");
  164. echo' xmlns:x="urn:schemas-microsoft-com:office:excel"
  165. xmlns="http://www.w3.org/TR/REC-html40">
  166. ';
  167. echo'
  168. echo'
  169. if(is_array($mapping))
  170. }
  171. $results=$this->findBySql('select * from '.$table);
  172. foreach($result as $key=>$val)
  173. echo'
  174. }
  175. '; ';
  176. {
  177. foreach($mapping as $key=>$val)
  178. echo'
  179. ';
  180. echo'
  181. ';
  182. foreach($results as $result)
  183. {
  184. echo'
  185. ';
  186. echo'
  187. '; ';
  188. echo'
  189. '.$val.'
    '.$val.'
    ';
  190. echo'';
  191. echo'';
  192. }
  193. function Backup($table)
  194. {
  195. if(is_array ($table))
  196. {
  197. $str="";
  198. foreach($table as $tab)
  199. $str.=$this->get_table_content($tab);
  200. return $str;
  201. }else{
  202. return $this->get_table_content($table);
  203. }
  204. }
  205. function Backuptofile($table,$file)
  206. {
  207. header("Content-disposition: filename=$file.sql");//所儲存的檔案名稱
  208. header("Content-type: application/octetstream");
  209. header("Pragma: no-cache");
  210. header("Expires: 0");
  211. if(is_array ($table))
  212. {
  213. $str="";
  214. foreach($table as $tab)
  215. $str.=$this->get_table_content($tab);
  216. echo $str;
  217. }else{
  218. echo $this->get_table_content($table);
  219. }
  220. }
  221. function Restore($table,$file="",$content="")
  222. {
  223. //排除file,content都為空白或者都不為空白的情況
  224. if(($file==""&&$content=="")||($file!=""&&$content!=""))
  225. echo"參數錯誤";
  226. $this->truncate($table);
  227. if($file!="")
  228. {
  229. if($this->RestoreFromFile($file))
  230. return true;
  231. else
  232. return false;
  233. }
  234. if($content!="")
  235. {
  236. if($this->RestoreFromContent($content))
  237. return true;
  238. else
  239. return false;
  240. }
  241. }
  242. //清空表,以便恢複資料
  243. function truncate($table)
  244. {
  245. if(is_array ($table))
  246. {
  247. $str="";
  248. foreach($table as $tab)
  249. $this->execute("TRUNCATE TABLE $tab");
  250. }else{
  251. $this->execute("TRUNCATE TABLE $table");
  252. }
  253. }
  254. function get_table_content($table)
  255. {
  256. $results=$this->findBySql("select * from $table");
  257. $temp = "";
  258. $crlf="
    ";
  259. foreach($results as $result)
  260. {
  261. /*(";
  262. foreach($result as $key=>$val)
  263. {
  264. $schema_insert .= " `".$key."`,";
  265. }
  266. $schema_insert = ereg_replace(",$", "", $schema_insert);
  267. $schema_insert .= ")
  268. */
  269. $schema_insert = "INSERT INTO $table VALUES (";
  270. foreach($result as $key=>$val)
  271. {
  272. if($val != "")
  273. $schema_insert .= " '".addslashes($val)."',";
  274. else
  275. $schema_insert .= "NULL,";
  276. }
  277. $schema_insert = ereg_replace(",$", "", $schema_insert);
  278. $schema_insert .= ");$crlf";
  279. $temp = $temp.$schema_insert ;
  280. }
  281. return $temp;
  282. }
  283. function RestoreFromFile($file){
  284. if (false !== ($fp = fopen($file, 'r'))) {
  285. $sql_queries = trim(fread($fp, filesize($file)));
  286. $this->splitMySqlFile($pieces, $sql_queries);
  287. foreach ($pieces as $query) {
  288. if(!$this->execute(trim($query)))
  289. return false;
  290. }
  291. return true;
  292. }
  293. return false;
  294. }
  295. function RestoreFromContent($content)
  296. {
  297. $content = trim($content);
  298. $this->splitMySqlFile($pieces, $content);
  299. foreach ($pieces as $query) {
  300. if(!$this->execute(trim($query)))
  301. return false;
  302. }
  303. return true;
  304. }
  305. function splitMySqlFile(&$ret, $sql)
  306. {
  307. $sql= trim($sql);
  308. $sql=split(';',$sql);
  309. $arr=array();
  310. foreach($sql as $sq)
  311. {
  312. if($sq!="");
  313. $arr[]=$sq;
  314. }
  315. $ret=$arr;
  316. return true;
  317. }
  318. }
  319. $db=new db();
  320. // 產生 word
  321. //$map=array('No','Name','Email','Age');
  322. //echo $db->toWord('test',$map,'檔案');
  323. // 產生 Excel
  324. //$map=array('No','Name','Email','Age');
  325. //echo $db->toExcel('test',$map,'檔案');
  326. // 產生 Xml
  327. //echo $db->toExtXml('test',0,20);
  328. // 產生 Json
  329. //echo $db->toExtJson('test',0,20);
  330. //備份
  331. //echo $db->Backuptofile('test','backup');
  332. ?>
複製代碼

2、整表備份 1 2 下一頁 尾頁

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.