Use PHP to implement XML backup of Mysql database _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags apache php
Use PHP to back up Mysql databases in XML format. The following is the file code used to back up a Mysql database using ApachePHP in Linux: File 1, Listtable. php (all tables in the database are listed as files for backup) select the following file code for backing 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

$ 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, which will not be described in detail here.

The file code used by PostgreSQL to back up the Mysql database: File 1, Listtable. PHP (the file lists all the tables in the database for backup selection) please select...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.