Use PHP to implement XML backup MySQL database favorites
The following is a file used to back up a MySQL database through Apache + PHP in Linux. Code :
File 1. listtable. php (the file lists all tables in the database for backup)
Select the table to back up:
Copy code The Code is as follows: <?
$ Con = mysql_connect ('localhost', 'root', 'xswlily ');
$ Lists = mysql_list_tables ("embed", $ con );
// Database connection code
$ I = 0;
While ($ I $ tb_name = mysql_tablename ($ lists, $ I );
Echo "". $ tb_name ."
";
// List all tables
$ I ++ ;}
?>
File 2. Backup. php Copy code The Code is as follows: <? If ($ table = "") header ("Location: listtable. php");?>
<?
$ Con = mysql_connect ('localhost', 'root', 'xswlily ');
$ Query = "select * from $ table ";
// Database Query
$ Result = mysql_db_query ("embed", $ query, $ con );
$ Filestr = "<"."? XML version = "1.0" encoding = "gb2312 "? "."> ";
$ Filestr. = "<". $ table. "S> ";
While ($ ROW = mysql_fetch_array ($ result ))
// List all records
{$ 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;
// The following is the file operation code
$ Filename = $ table. ". xml ";
$ Fp = fopen ("$ FILENAME", "W ");
Fwrite ($ FP, $ filestr );
Fclose ($ FP );
Echo "data table". $ table. "backup successful! ";?>
you can back up the selected tables in the database by using the preceding operations.
the preceding section describes how to back up an XML database using PHP. It is not complicated. With XML, we can back up a variety of databases, of course, you can also restore the backed up XML file to the database through relevant methods, which will not be described in detail here.