歡迎您光臨本站 註冊首頁

用PHP實現XML備份Mysql資料庫

←手機掃碼閱讀     火星人 @ 2014-03-12 , reply:0
  本文出自:http://www.webrj.com

以下是在Linux下通過Apache+PHP對Mysql資料庫的備份的文件代碼:

文件一、Listtable.php (文件列出資料庫中的所有表格,供選擇備份)



<br>使用XML備份Mysql資料庫


請選擇要備份的表格:
$con=mysql_connect('localhost','root','xswlily');
$lists=mysql_list_tables("embed",$con);
//資料庫連接代碼
$i=0;
while($i$tb_name=mysql_tablename($lists,$i);
echo "".$tb_name."
";
//列出所有的表格
$i++;}

?>




文件二、Backup.php



<br>使用XML備份Mysql資料庫


$con=mysql_connect('localhost','root','xswlily');
$query="select * from $table ";
//資料庫查詢
$result=mysql_db_query("embed",$query,$con);
$filestr="<"."?xml version=\"1.0\" encoding=\"GB2312\"?".">";
$filestr.="<".$table."s>";
while ($row=mysql_fetch_array($result))
//列出所有的記錄
{$filestr.="<".$table.">";
$fields=mysql_list_fields("embed",$table,$con);
$j=0;
//$num_fields=mysql_field_name($fields,$j);
//echo $num_fields;
while ($j$num_fields=mysql_field_name($fields,$j);
$filestr.="<".$num_fields.">";
$filestr.=$row[$j];
$filestr.="";
$j++;}
$filestr.="";
}
$filestr.="";
echo $filestr;
//以下是文件操作代碼
$filename=$table.".xml";
$fp=fopen("$filename","w");
fwrite($fp,$filestr);
fclose($fp);
Echo "數據表".$table."已經備份成功!";?>



通過以上文件的操作就可以實現對資料庫中選定的表格進行備份.

以上主要介紹了通過PHP實現XML備份資料庫的操作方法,其實並不複雜,通過XML,我們可以備份各種各樣的資料庫,當然也可以通過相關的方法將備份的XML文檔恢復到資料庫中,這裡就不詳細描述了。





[火星人 ] 用PHP實現XML備份Mysql資料庫已經有707次圍觀

http://coctec.com/docs/program/show-post-71976.html