用PHP實(shí)現(xiàn)XML備份Mysql數(shù)據(jù)庫(kù)
以下是在Linux下通過(guò)Apache+PHP對(duì)Mysql數(shù)據(jù)庫(kù)的備份的文件代碼:
文件一、Listtable.php (文件列出數(shù)據(jù)庫(kù)中的所有表格,供選擇備份)
請(qǐng)選擇要備份的表格: <? $con=mysql_connect('localhost','root','xswlily'); $lists=mysql_list_tables('embed',$con); //數(shù)據(jù)庫(kù)連接代碼 $i=0; while($i$tb_name=mysql_tablename($lists,$i); echo ''.$tb_name.' '; //列出所有的表格 $i++;}
?>
文件二、Backup.php <?if ($table=='') header('Location:listtable.php');?>
<? $con=mysql_connect('localhost','root','xswlily'); $query='select * from $table '; //數(shù)據(jù)庫(kù)查詢(xún) $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 '數(shù)據(jù)表'.$table.'已經(jīng)備份成功!';?>
通過(guò)以上文件的操作就可以實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)中選定的表格進(jìn)行備份.
以上主要介紹了通過(guò)PHP實(shí)現(xiàn)XML備份數(shù)據(jù)庫(kù)的操作方法,其實(shí)并不復(fù)雜,通過(guò)XML,我們可以備份各種各樣的數(shù)據(jù)庫(kù),當(dāng)然也可以通過(guò)相關(guān)的方法將備份的XML文檔恢復(fù)到數(shù)據(jù)庫(kù)中,這里就不詳細(xì)描述了。
