Php database backup script _ PHP Tutorial

Source: Internet
Author: User
Tags php database
Php database backup script. Php database backup script code :? Php backup database $ hostlocalhost; $ userroot; database account $ password; database password $ dbname php database backup script

Php database backup script

The code is as follows:

  

// Back up the database

$ Host = "localhost ";

$ User = "root"; // database account

$ Password = ""; // database password

$ Dbname = "mysql"; // database name

// The account, password, and name are all uploaded from the page.

If (! Mysql_connect ($ host, $ user, $ password) // connect to the mysql database

{

Echo 'database connection failed. check the database and try again ';

Exit;

}

If (! Mysql_select_db ($ dbname) // check whether the database exists

{

Echo 'database does not exist: '. $ dbname.'. check the database and try again ';

Exit;

}

Mysql_query ("set names 'utf8 '");

$ Mysql = "set charset utf8 ;";

$ 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']. ";";

$ 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 );";

}

}

$ Filename = $ dbname. date ('ymjgi '). ". SQL"; // storage path, which is stored in the outermost layer of the project by default

$ Fp = fopen ($ filename, 'w ');

Fputs ($ fp, $ mysql );

Fclose ($ fp );

Echo "data backup succeeded ";

?>

The backup script code for the PostgreSQL database is as follows :? Php // backup database $ host = localhost; $ user = root; // database account $ password =; // database password $ dbname...

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.