PHP数据库备份函数

<? 
//数据库备份函数
function backup($host,$user,$pass,$db,$table){
$conn=mysql_connect($host,$user,$pass); 
$query="select * from ".$table; 
//数据库查询 
$enter=chr(13).chr(10);//表示“\r\n”;
$result=mysql_db_query($db,$query,$conn); 
$filestr="<?xml version=\"1.0\" encoding=\"GB2312\"?>".$enter; 
$filestr.="<".$table."s>".$enter; 

while ($row=mysql_fetch_array($result)){//列出所有的记录 
$filestr.="<".$table.">".$enter; 
$fields=mysql_list_fields($db,$table,$conn); 
$j=0;
while($num_fields=@mysql_field_name($fields,$j)){
$filestr.="<".$num_fields.">";
$filestr.=htmlspecialchars($row[$j]); 
$filestr.="</".$num_fields.">".$enter;
$j++;
} 
$filestr.="</".$table.">".$enter;
} 
$filestr.="</".$table."s>".$enter;
//以下是文件操作代码 
$filename=$table.".xml"; 
$fp=fopen("$filename","w"); 
fwrite($fp,$filestr); 
fclose($fp); 
Echo "数据表".$table."已经备份成功!";
}
//例子backup('localhost','root','','sq','sy');
?> 

 

你可能感兴趣的:(PHP,mysql,xml,J#,FP)