PHP Database backup Script

Source: Internet
Author: User

PHP Database backup Script

The code is as follows:

<?php

Backing up a database

$host = "localhost";

$user = "root"; Database account

$password = ""; Database Password

$dbname = "MySQL"; Database name

The account number, password, and name are all sent from the page.

if (!mysql_connect ($host, $user, $password))//connect to MySQL Database

{

Echo ' Database connection failed, please check and try again ';

Exit

}

if (!mysql_select_db ($dbname))//whether the database exists

{

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

Exit

}

mysql_query ("Set names ' UTF8 '");

$mysql = "Set charset UTF8;";

$q 1 = mysql_query ("Show Tables");

while ($t = mysql_fetch_array ($q 1))

{

$table = $t [0];

$q 2 = mysql_query ("Show create Table ' $table '");

$sql = Mysql_fetch_array ($q 2);

$mysql. = $sql [' Create Table ']. "; ";

$q 3 = mysql_query ("select * from ' $table");

while ($data = Mysql_fetch_assoc ($q 3))

{

$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"; Store path, default to project outermost

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

Fputs ($fp, $mysql);

Fclose ($FP);

echo "Data backup succeeded";

?>

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.