PHP backup database for SQL files and download source

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags backup code create data database structure dedecms download file

This is a backup of all the tables in the specified database as an SQL file that can be downloaded. This source code from dedecms procedures, functions quite a lot, but also very useful, but the quality of the code needs to be improved

<!? php / ****** backup database structure ****** / / * function name: table2sql () function: the structure of the table converted to SQL function parameters: $ table: to extract the table return value: returns the result of the extraction, SQL set of functions: heiyeluren * / function table2sql ($ table) {global $ db; $ tabledump = "DROP TABLE IF EXISTS $ table; n"; $ createtable = $ db-- -> query ("SHOW CREATE TABLE $ table"); $ create = $ db-> fetch_row ($ createtable); $ tabledump. = $ create [1]. "nn"; return $ tabledump;} / *** *** backup database structure and all data ****** / / * function name: data2sql () function function: the table structure and data into SQL function parameters: $ table: to be extracted table name return value : return the result of the extraction, SQL aggregate functions author: heiyeluren * / function data2sql ($ table) {global $ db; $ tabledump = "DROP TABLE IF EXISTS $ table; n"; $ createtable = $ db-> query ("SHOW CREATE TABLE $ table"); $ create = $ db $ rows = $ db-> query ("SELECT * FROM $ table"); $ numfields = $ db-> num_fields ($ CREATE TABLE) $ rows = $ db-> num_rows ($ rows); while ($ row = $ db-> fetch_row ($ rows)) {$ comma = ""; $ tabledump. = "INSERT INTO $ table VALUES ( mysql_escape_string ($ row [$ i]). '' "; $ comma =", "" $ ($ i = 0; $ i <$ numfields; $ i ++) {$ tabledump. = $ comma. "' ;} $ tabledump. = "); n";} $ tabledump. = "n"; return $ tabledump;}?> <! -? php $ host = "localhost"; // host Name $ user = "root"; // MYSQL user name $ password = "root"; // password $ dbname = "dedecmsv4"; // backup database mysql_connect ($ host, $ user, $ password); mysql_select_db $ q1 = mysql_query ("show table"); while ($ t = mysql_fetch_array ($ q1)) {$ table = $ t [0]; $ q2 = mysql_query ; $ mysql. = $ sql ['Create Table']. "; rnrn"; # DDL $ q3 = mysql_query ("select * from` $ table` "); while ($ data $ keys = array_keys ($ data); $ keys = array_map ('addslashes', $ keys); $ keys = join ('`,`', $ keys); $ keys = "` $ vals = array_values ​​($ data); $ vals = array_map ('addslashes', $ vals); $ vals = join ("','", $ vals); $ vals = " $ vals. "" "$ mysql. =" insert into $ table` ($ keys) values ​​($ vals); rn ";} $ mysql. =" rn ";} $ filename = date ('Ymd'). "_". $ dbname. ". sql"; // filename is the date of the day $ fp = fopen ($ filename, 'w'); fputs ($ fp, $ mysql); fclose ($ fp); echo "Data backup success, generate a backup file." $ filename;?>
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.