php 備份數據庫類
<?php/****** 備份數據庫結構 ******//****正好要研究如何備份數據庫,分享一個php實現MYSQL備份的類庫********/ /* 函數名稱:table2sql() 函數功能:把表的結構轉換成為SQL 函數參數:$table: 要進行提取的表名 返 回 值:返回提取后的結果,SQL集合 函數作者:heiyeluren */ function table2sql($table) { global $db; $tabledump = 'DROP TABLE IF EXISTS $table;n'; $createtable = $db->query('SHOW CREATE TABLE $table'); $create = $db->fetch_row($createtable); $tabledump .= $create[1].';nn'; return $tabledump; } /****** 備份數據庫結構和所有數據 ******/ /* 函數名稱:data2sql() 函數功能:把表的結構和數據轉換成為SQL 函數參數:$table: 要進行提取的表名 返 回 值:返回提取后的結果,SQL集合 函數作者:heiyeluren */ function data2sql($table) { global $db; $tabledump = 'DROP TABLE IF EXISTS $table;n'; $createtable = $db->query('SHOW CREATE TABLE $table'); $create = $db->fetch_row($createtable); $tabledump .= $create[1].';nn'; $rows = $db->query('SELECT * FROM $table'); $numfields = $db->num_fields($rows); $numrows = $db->num_rows($rows); while ($row = $db->fetch_row($rows)) { $comma = ''; $tabledump .= 'INSERT INTO $table VALUES('; for($i = 0; $i < $numfields; $i++) { $tabledump .= $comma.'’'.mysql_escape_string($row[$i]).'’'; $comma = ','; } $tabledump .= ');n'; } $tabledump .= 'n'; return $tabledump; }?>
相關文章: