A simple PHP database backup program _php Tutorial

Source: Internet
Author: User
Tags php database
PHP backup MySQL database is a lot of personal webmaster site features, of course, if you want to back up a few gigabytes or larger database when the PHP backup method feel a little hard.

PHP backup MySQL Database source code, in the perfect Php+mysql project, in the background will have backup MySQL database function, with this feature, we can no longer use FTP or use MySQL management tool to download MySQL database, very convenient, for want to To do such a function of phper, in fact, the principle is not very troublesome, mainly has the following three points:

One, be sure to connect to the database, so that the SQL statement can be printed out the MySQL data table, two, through the PHP file operation function database operations, including the creation of a folder to save the MySQL database, this step is mainly the steps to create a new file, three, the MySQL database to save.

Through this principle, we can make a backup database of their own functions, the following is a PHP database backup source code, the main structure is based on the above three points, the source is composed of several methods, we can also encapsulate it into its own PHP class. Extrapolate, hope that phper on this basis to design a suitable for their own MySQL database backup function source code

The code is as follows Copy Code

/** backup Database build. sql file
* @param $browseinfo String Browser version
* Return $browseinfo
*/
function Createsql () {
Create a date
$timer 1 = time ();
$path = "my_sql/";
$content =gettables ();
$filename = $path. $timer 1. ". SQL ";

Determine if the folder is not in
if (!file_exists ($path)) {
If this directory does not exist, 0777 indicates the maximum read and write permissions
if (mkdir ($path, 0777)) {
echo "New build Directory";
}
}

Determine if a file exists
if (!file_exists ($filename)) {
If the file does not exist, create the file
@fopen ($filename, "w");

Determine if the file is writable
if (is_writable ($filename)) {
Open file stream with add as "a" mode
if (! $handle = fopen ($filename, "a")) {
echo "File is not open";
Exit ();
}

if (!fwrite ($handle, $content)) {
echo "File is not writable";
Exit ();
}

Close file stream
Fclose ($handle);
echo "Generate file and save first content";

}else {
echo "File $filename not writable";
}
}else{
if (is_writable ($filename)) {
To open a file stream as an added method
if (! $handle = fopen ($filename, "a")) {
echo "File is not open";
Exit ();
}
Fclose ($handle);
}else{
echo "File $filename not writable";
}
}
}


/**
* Get the table name in the database
* Return $STR SQL statements that generate database build tables and insert values
*/
function Gettables () {
$mysqli = new Mysqli ("localhost", "root", "" "," BBS ");
$str = ";
if ($result = $mysqli->query ("SHOW TABLES")) {
while ($row = $result->fetch_row ()) {
$str. = Data2sql ($row [0]). "
";
}
$mysqli->close ();
return $str;
}

}

/**
* Get table structure and values in the database
* Return $TABLEDUMP SQL statement that returns the structure and value of a table
*/
function Data2sql ($table) {
$mysqli = new Mysqli ("localhost", "root", "" "," BBS ");
/* Check connection */
if (Mysqli_connect_errno ()) {
printf ("Connect failed:%sn", Mysqli_connect_error ());
Exit ();
}
$tabledump = "DROP TABLE IF EXISTS $table; n";
$result = $mysqli->query ("SHOW CREATE TABLE $table");
$create = $result->fetch_row ();
$tabledump. = $create [1]. "; nn ";

$rows = $mysqli->query ("SELECT * from $table");
$numfields = $rows->num_rows;

while ($row = $rows->fetch_row ()) {
$comma = "";
$tabledump. = "INSERT into $table VALUES (";
for ($i = 0; $i < $numfields; $i + +)
{
$tabledump. = $comma. "'". Mysql_escape_strin
G ($row [$i]). "'";
$comma = ",";
}
$tabledump. = "); n";
}
$tabledump. = "n";

return $tabledump;
}
?>

http://www.bkjia.com/PHPjc/632928.html www.bkjia.com true http://www.bkjia.com/PHPjc/632928.html techarticle PHP backup MySQL database is a lot of personal webmaster site features, of course, if you want to back up a few gigabytes or larger database when the PHP backup method feel a little hard. PHP Backup Mys ...

  • 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.