How to Use php to automatically back up database tables and use php to back up database tables

Source: Internet
Author: User

How to Use php to automatically back up database tables and use php to back up database tables

1. Preface

There are many backup methods for mysql databases;

For example:

1. Use the mysqldump Function

Mysqldump-u username-p dbname table1 table2...> BackupName. SQL

The dbname parameter indicates the database name.

The table1 and table2 parameters indicate the names of the tables to be backed up. If this parameter is left blank, the entire database is backed up;

BackupName. SQL parameter table design the name of the backup file. You can add an absolute path before the file name. Generally, the database is divided into a file with the suffix "SQL;

Basic usage:

2. Management Tools

There are many methods to back up the database, the above two are more common

This article mainly explains how to use php functions to automatically back up databases.

2. Related functions

2.1. fopen

Http://www.w3school.com.cn/php/func_filesystem_fopen.asp for details

2.2. array_keys

Http://www.w3school.com.cn/php/func_array_keys.asp for details

2.3. array_values

2.4. implode

Http://www.w3school.com.cn/php/func_string_implode.asp for details

2.5. substr

Http://www.w3school.com.cn/php/func_string_substr.asp for details

2.6. fwrite

Https://www.w3cschool.cn/php/func-filesystem-fwrite.html for details

3. Implementation ideas

4. code structure

/*** [CopyDb description] backup database * @ param [type] $ dbname [description] database name * @ param [type] $ fileName [description] storage file name * @ return [type] [description] */public function copyDb ($ dbname, $ fileName) {$ myfile = fopen ($ fileName, "w") or die ("Unable to open file! "); // Open the storage file $ this-> link-> query (" use {$ dbname} "); // switch the database $ this-> changeDb ($ dbname ); $ tables = $ this-> link-> query ('show tables '); // obtain the names of all tables in the current database while ($ re = $ tables-> fetch (PDO :: FETCH_ASSOC) {// var_dump ($ re); // view the array structure $ tableName = $ re ['tables _ in _'. $ dbname]; // a specific subscript $ SQL = "show create table {$ tableName};"; $ tableSql = $ this-> link-> query ($ SQL ); fwrite ($ myfile, "drop table if exists '{$ tableName}'; \ r \ n"); // Add the following Backup TABLE structure when the TABLE is deleted by default, execute this loop once while ($ re = $ tableSql-> fetch (PDO: FETCH_ASSOC) {// echo "<pre>"; // var_dump ($ re ); // echo "</pre>"; echo "backing up the structure of the Table {$ re ['table']} <br/>"; fwrite ($ myfile, $ re ['create table']. "; \ r \ n"); echo "backing up the {$ re ['table']} structure <br/> ";} // The following Backup table data echo "backing up table {$ tableName} data <br/>"; $ SQL = "select * from {$ tableName };"; $ valueSql = $ this-> link-> query ($ SQL); while ($ re = $ valueSql-> fetch (PDO: FETCH_ASSOC )) {$ keyArr = array_keys ($ re); // obtain the corresponding key value $ valueArr = array_values ($ re); // obtain the corresponding value $ keyStr = ''; foreach ($ keyArr as $ key => $ value) {$ keyStr. = "'". $ value. "'," ;}$ keyStr = substr ($ keyStr, 0, strlen ($ keyStr)-1); // retrieve the last comma $ valueStr = ''; // var_dump ($ valueArr); foreach ($ valueArr as $ key => $ value) {$ valueStr. = "'". $ value. "',";} // The preceding statement is for the corresponding SQL statement $ valueStr = substr ($ valueStr, 0, strlen ($ valueStr)-1 ); // retrieve the last comma $ SQL = "insert into '{$ tableName}' ({$ keyStr}) values ({$ valueStr})"; fwrite ($ myfile, $ SQL. "; \ r \ n");} echo "backing up table {$ tableName} data completed <br/> "; echo "<br/> 

5. Conclusion

The main process of backing up a database:

Switch to the corresponding database;

Use show create table tableName to get the table structure and write it to the file;

Query all the table data, generate the corresponding SQL statement cyclically, and write it to the file;

SQL file generated during trial run;

The above implementation method of using php to automatically back up database tables is all the content that I have shared with you. I hope you can give us a reference and support for more.

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.