This article describes how to use XML to back up MySQL database instances in PHP. This method is applicable to users who only have small data volumes and have low security requirements.
The following is the file code for backing up a Mysql database through Apache + PHP in Linux:
File 1. Listtable. php (the file lists all tables in the database for backup)
Select the table to back up:
| The Code is as follows: |
Copy code |
$ 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
| The Code is as follows: |
Copy code |
"") 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 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.