PHP back up the database to generate the SQL file and download the function code

Source: Internet
Author: User

Copy codeThe Code is as follows:
<!? Php
/****** Back up the database structure ******/
/*
Function Name: table2sql ()
Function: converts the table structure to SQL
Function parameter: $ table: name of the table to be extracted
Return Value: return the extracted results, SQL set
Function Author: 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;
}
/****** Back up the database structure and all data ******/
/*
Function Name: data2sql ()
Function: converts the table structure and data into SQL statements.
Function parameter: $ table: name of the table to be extracted
Return Value: return the extracted results, SQL set
Function Author: heiyeluren
*/
Function data2sql ($ 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 ";
$ Rows = $ db-> query ("SELECT * FROM $ table ");
$ Numfields = $ db-> num_fields ($ rows );
$ Numrows = $ db-> num_rows ($ rows );
While ($ row = $ db-> fetch_row ($ rows ))
{
$ Comma = "";
$ Tabledump. = "insert into $ table VALUES (";
For ($ I = 0; $ I <$ numfields; $ I ++)
{
$ Tabledump. = $ comma. "'". mysql_escape_string ($ row [$ I]). "'";
$ Comma = ",";
}
$ Tabledump. = "); n ";
}
$ Tabledump. = "n ";
Return $ tabledump;
}
?>
<! --? Php
$ Host = "localhost"; // host name
$ User = "root"; // MYSQL Username
$ Password = "root"; // password
$ Dbname = "dedecmsv4"; // backup database
Mysql_connect ($ host, $ user, $ password );
Mysql_select_db ($ dbname );
$ Q1 = mysql_query ("show tables ");
While ($ t = mysql_fetch_array ($ q1 )){
$ Table = $ t [0];
$ Q2 = mysql_query ("show create table '$ table '");
$ SQL = mysql_fetch_array ($ q2 );
$ Mysql. = $ SQL ['create table']. "; rnrn"; # DDL
$ Q3 = mysql_query ("select * from '$ table '");
While ($ data = mysql_fetch_assoc ($ q3 ))
{
$ Keys = array_keys ($ data );
$ Keys = array_map ('addslashes', $ keys );
$ Keys = join ('','', $ keys );
$ 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 ('ymmd'). "_". $ dbname. ". SQL"; // the file name is the date of the current day.
$ Fp = fopen ($ filename, 'w ');
Fputs ($ fp, $ mysql );
Fclose ($ fp );
Echo "Data Backup successful, backup file generated". $ filename;
?>

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.