Phpmysql database backup and data restoration class _ PHP Tutorial

Source: Internet
Author: User
Tags mysql tutorial
Phpmysql database backup and data restoration class. Note: This class applies to database backup for small websites and built-in MYSQL connections. you only need to configure the data connection and storage backup location. * Description of real-name classes and database connection. this class is applicable to database backup for small websites and has built-in MYSQL connections. you only need to configure the data connection and storage backup location. * Perform the following operations after the class is real-listed and connected to the database

Php Tutorial mysql tutorial database tutorial backup and data restoration
/**
* Description: This type is applicable to database backup for small websites and built-in mysql connections. you only need to configure data connections simply.
* And the storage backup location.
* Perform the following operations after the class is real-listed and connected to the database
* Get_db_table ($ database) obtains all data tables.
* Export_ SQL ($ table, $ subsection = 0) generates an SQL file. Note that the generated SQL file is saved only to the server directory and is not downloaded.
* Import_ SQL ($ dir) only import SQL files in the server directory to restore data
* This type is easy to create and can be spread at will. if you have any suggestions for this type, please send an email to the shrimps.
* @ Author Zhao Hongjian [Youtian shrimp]
* Email: 328742379@qq.com
* Qq chat group: 69574955 juyitang-online page creation
*/

Class data {
Public $ data_dir = "class/"; // path of the backup file
Public $ transfer = ""; // temporarily store SQL statements. [do not assign values to this attribute. Otherwise, an incorrect SQL statement is generated.]

/**
* Database connection
* @ Param string $ host name
* @ Param string $ user username
* @ Param string $ pwd password
* @ Param string $ db Select the database name
* @ Param string $ charset encoding method
*/
Function connect_db ($ host, $ user, $ pwd, $ db, $ charset = 'gbk '){
If (! $ Conn = mysql_connect ($ host, $ user, $ pwd )){
Return false;
}
Mysql_select_db ($ db );
Mysql_query ("set names $ charset ");
Return true;
}

/**
* Generate SQL statements
* @ Param $ table the table to be backed up
* @ Return $ tabledump: SQL statement generated
*/
Public function set_ SQL ($ table, $ subsection = 0, & $ tabledom = ''){
$ Tabledom. = "drop table if exists $ tablen ";
$ Createtable = mysql_query ("show create table $ table ");
$ Create = mysql_fetch_row ($ createtable );
$ Create [1] = str_replace ("n", "", $ create [1]);
$ Create [1] = str_replace ("t", "", $ create [1]);

$ Tabledom. = $ create [1]. "; n ";

$ Rows = mysql_query ("select * from $ table ");
$ Numfields = mysql_num_fields ($ rows );
$ Numrows = mysql_num_rows ($ rows );
$ N = 1;
$ Sqlarry = array ();
While ($ row = mysql_fetch_row ($ rows )){
$ Comma = "";
$ Tabledom. = "insert into $ table values (";
For ($ I = 0; $ I <$ numfields; $ I ++)
{
$ Tabledom. = $ comma. "'". mysql_escape_string ($ row [$ I]). "'";
$ Comma = ",";
}
$ Tabledom. = ") n ";
If ($ subsection! = 0 & strlen ($ this-> transfer) >=$ subsection * 1000 ){
$ Sqlarry [$ n] = $ tabledom;
$ Tabledom = ''; $ n ++;
}
}
Return $ sqlarry;
}

/**
* List tables in a database
* @ Param database $ name of the database to be operated on
* @ Return array $ database table in the dbarray list
*/
Public function get_db_table ($ database ){
$ Result = mysql_list_tables ($ database );
While ($ tmparry = mysql_fetch_row ($ result )){
$ Dbarry [] = $ tmparry [0];
}
Return $ dbarry;
}

/**
* Verify whether the directory is valid
* @ Param diretory $ dir
* @ Return booln
*/
Function check_write_dir ($ dir ){
If (! Is_dir ($ dir) {@ mkdir ($ dir, 0777 );}
If (is_dir ($ dir )){
If ($ link = opendir ($ dir )){
$ Filearry = scandir ($ dir );
For ($ I = 0; $ I If ($ filearry [$ I]! = '.' | $ Filearry! = '..'){
@ Unlink ($ dir. $ filearry [$ I]);
}
}
}
}
Return true;
}
/**
* Write data to a file
* @ Param file $ filename file name
* @ Param string $ str information to be written
* @ Return booln return true if the write is successful; otherwise, false
*/
Private function write_ SQL ($ filename, $ str ){
$ Re = true;
If (! @ $ Fp = fopen ($ filename, "w +") {$ re = false; echo "an error occurred while opening the file. backup failed! ";}
If (! @ Fwrite ($ fp, $ str) {$ re = false; echo "an error occurred while writing information. backup failed! ";}
If (! @ Fclose ($ fp) {$ re = false; echo "an error occurred while closing the file. backup failed! ";}
Return $ re;
}

/**
* Generate an SQL file
* @ Param string $ SQL statement
* @ Param number $ subsection volume size, in kb, 0 indicates no volume sharding
*/
Public function export_ SQL ($ table, $ subsection = 0 ){
If (! $ This-> check_write_dir ($ this-> data_dir) {echo 'You do not have permission to operate the Directory, backup failed '; return false ;}
If ($ subsection = 0 ){
If (! Is_array ($ table )){
$ This-> set_ SQL ($ table, 0, $ this-> transfer );
} Else {
For ($ I = 0; $ I $ This-> set_ SQL ($ table [$ I], 0, $ this-> transfer );
}
}
$ Filename = $ this-> data_dir.date ("ymd", time (). '_ all. SQL ';
If (! $ This-> write_ SQL ($ filename, $ this-> transfer) {return false ;}
} Else {
If (! Is_array ($ table )){
$ Sqlarry = $ this-> set_ SQL ($ table, $ subsection, $ this-> transfer );
$ Sqlarry [] = $ this-> transfer;
} Else {
$ Sqlarry = array ();
For ($ I = 0; $ I $ Tmparry = $ this-> set_ SQL ($ table [$ I], $ subsection, $ this-> transfer );
$ Sqlarry = array_merge ($ sqlarry, $ tmparry );
}
$ Sqlarry [] = $ this-> transfer;
}
For ($ I = 0; $ I $ Filename = $ this-> data_dir.date ("ymd", time (). '_ part'. $ I.'. SQL ';
If (! $ This-> write_ SQL ($ filename, $ sqlarry [$ I]) {return false ;}
}
}
Return true;
}
/**
* Load an SQL file
* @ Param diretory $ dir
* @ Return booln
* Note: Do not store other files in the directory or directory.
* To save recovery time
*/
Public function import_ SQL ($ dir ){
If ($ link = opendir ($ dir )){
$ Filearry = scandir ($ dir );
$ Pattern = "_ part [0-9] +. SQL $ | _ all. SQL $ ";
For ($ I = 0; $ I If (eregi ($ pattern, $ filearry [$ I]) {
$ Sqls = file ($ dir. $ filearry [$ I]);
Foreach ($ sqls as $ SQL ){
Str_replace ("r", "", $ SQL );
Str_replace ("n", "", $ SQL );
If (! Mysql_query (trim ($ SQL) return false;
}
}
}
Return true;
}
}

}

Application method

// $ D = new data ();

// Connect to the database
// If (! $ D-> connect_db ('localhost', 'root', '', 'guestbook', 'gbk ')){
// Echo 'database connection failed ';
//}

// Search for all data tables in the database
// $ Tablearry = $ d-> get_db_table ('guestbook ');

// Back up and generate an SQL file
// If (! $ D-> export_ SQL ($ tablearry )){
// Echo 'backup failed ';
//} Else {
// Echo 'backup successful ';
//}

// Restore the imported SQL folder
// If ($ d-> import_ SQL ($ d-> data_dir )){
// Echo 'recovery successful ';
//} Else {
// Echo 'recovery failed ';
//}

SNAPSHOT * and storage backup location. * The class is real-listed and connected to the database...

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.