Back up the MySQL database with PHP and XML. The following is the file code for backing up a Mysql database using Apache + PHP in Linux: File 1, Listtable. php (all tables in the database are listed as files for backup) select the following file code to back up the Mysql database using Apache + PHP in Linux:
File 1. Listtable. php (the file lists all tables in the database for backup)
Select the table to back up:
$ 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
"
") & Nbspheader (
"Location: listtable. php
");?>
$ Con = mysql_connect (
'Localhost
',
'Root
',
'Xswlily
');
$ Query =
"Select * & nbspfrom $ table
";
// Database query
$ Result = mysql_db_query (
"Embed
", $ Query, $ con );
$ Filestr =
"<
".
"? Xml & nbspversion =
"1.0
"& Nb1_coding =
"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.
"Backed up successfully!
";?>
You can back up the selected tables in the database by using the above file 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.
Example file 1 and Listtable. php (all tables in the database are listed in the file for backup selection) please select...