PHP Mysql Data Backup

Source: Internet
Author: User
Tags php mysql

Functions:
Require_once ("backdata. Class. php ");
$ Link = @ mysql_connect ("localhost", "Database Name", "password") or die ('could not connect to server .');
Mysql_query ("use CMS", $ link );
Mysql_query ("set names utf8", $ link );
$ Dbbck = new backupdata ($ link); // instantiate it. You only need a link ID.
// If you want to back up all the tables in a database during data backup, write as follows:
$ Dbbck-> backuptables ("CMS", "./", array ('*'));

// When backing up data, if you want to back up only one table in a database, you can write as follows:
$ Dbbck-> backuptables ("CMS", "./", array ('user '));

// When backing up data, if you want to back up multiple tables in a database, you can write as follows:

$ Dbbck-> backuptables ("CMS", "./", array ('user', 'acl', 'in oin '));
// Annotation: $ dbbck-> backuptables ("parameter 1", "parameter 2", array,
Parameter 1: Database Name,
Parameter 2: Location of the backup data to be stored (that is, the directory address)
Third: the tables you want to save
OK...

The following are Code : Copy code The Code is as follows: <? PHP
/*
*
* A simple backup data class
* Author FC
*
*/
Class backupdata {
Private $ mysql_link; // link ID
Private $ dbname; // Database Name
Private $ datadir; // directory where data is stored
Private $ tablenames; // table name
Public Function _ construct ($ mysql_link ){

$ This-> mysql_link = $ mysql_link;
}

Public Function backuptables ($ dbname, $ datadir, $ tablenames) {// start backup

$ This-> dbname = $ dbname;
$ This-> datadir = $ datadir;
$ This-> tablenames = $ tablenames;
$ Tables = $ this-> delarray ($ this-> tablenames );
$ Sqls = '';
Foreach ($ tables as $ tablename ){
If ($ tablename = '') {// The table does not exist
Continue;
}
****** ********
// If a table exists, delete it first.
$ Sqls. = "Drop table if exists $ tablename; \ n ";
// Read the table structure
$ Rs = mysql_query ("show create table $ tablename", $ this-> mysql_link );
$ ROW = mysql_fetch_row ($ RS );
// Obtain the table structure as an SQL statement
$ Sqls. = $ row ['1']. "; \ n ";
Unset ($ RS );
Unset ($ row );
// ************************ The second half of the SQL statement is formed ******* *******
// Query all the data in the table
$ Rs = mysql_query ("select * from $ tablename", $ this-> mysql_link );
// Number of fields in the table
$ Field = mysql_num_fields ($ RS );
// Form this SQL statement: "insert into 'groups' values ('1499e0ca25988d', 'directory','', '0 ');"
While ($ rows = mysql_fetch_row ($ RS )){
$ Comma = ''; // comma
$ Sqls. = "insert into '$ tablename' values (";
For ($ I = 0; $ I <$ field; $ I ++ ){
$ Sqls. = $ Comma. "'". $ rows [$ I]. "'";
$ Comma = ',';
}
$ Sqls. = "); \ n ";
}
}
$ Backfilepath = $ this-> datadir. date ("ymdhis", time (). '. SQL ';
// Write a file
$ Filehandle = fopen ($ backfilepath, "W ");
Fwrite ($ filehandle, $ sqls );
Fclose ($ filehandle );
}

Private function delarray ($ array) {// process the incoming Array
Foreach ($ array as $ tables ){
If ($ tables = '*') {// All Tables (the table name cannot be an array in the conventional way)
$ Newtables = mysql_list_tables ($ this-> dbname, $ this-> mysql_link );
$ Tablelist = array ();
For ($ I = 0; $ I <mysql_numrows ($ newtables); $ I ++ ){
Array_push ($ tablelist, mysql_tablename ($ newtables, $ I ));
}
$ Tablelist = $ tablelist;
} Else {
$ Tablelist = $ array;
Break;
}
}
Return $ tablelist;
}
}
Related Article

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.