Home > Backend Development > PHP Tutorial > A simple php database backup program_PHP tutorial

A simple php database backup program_PHP tutorial

WBOY
Release: 2016-07-13 10:46:49
Original
906 people have browsed it

php backup mysql database is a function that many personal webmaster websites have. Of course, if you want to back up a database of several GB or larger, the php backup method feels a bit difficult.

PHP backs up the source code of the mysql database. In a complete PHP+Mysql project, there will be a function of backing up the Mysql database in the background. With this function, we no longer need to use FTP or use mysql management tools to perform mysql database Downloaded, very convenient. For phpers who want to do this function, the principle is actually not very troublesome. There are three main points:

First, you must connect to the database so that you can print out the mysql data table through SQL statements. Second, perform database operations through PHP's file operation functions, including creating a folder to save the mysql database. This step is mainly to create a new file. Step three, save the mysql database.

Through this principle, we can create a backup database function of our own. The following is the source code of a PHP database backup. The main structure is based on the three points above. The source code is composed of several methods. Composed, we can also encapsulate it into our own php class. Drawing inferences from one example, I hope that phper can design a suitable source code for mysql database backup function on this basis

The code is as follows Copy code

/**Back up database and generate .sql file
* @param $browseinfo String Browser version
* return $browseinfo
​​*/
Function createsql(){
//Create a date
          $timer1 = time();
          $path = "my_sql/";
$content =gettables();
          $filename = $path.$timer1.".sql";

//First determine whether the folder exists
            if(!file_exists($path)){
//If this directory does not exist, 0777 indicates the maximum read and write permissions
If(mkdir($path,0777)){
//echo "New directory";
            }
}

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

//Determine whether the file is writable
If(is_writable($filename)){
                                          //Open the file in the adding mode, that is, open the file stream in the "a" mode
If(!$handle = fopen($filename,"a")){
echo "File cannot be opened";
exit();
                }

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

//Close the file stream
          fclose($handle);
              echo "Generate file and save the first content";
         
}else {
echo "The file $filename is not writable";
}
         }else{
If(is_writable($filename)){
//Open the file stream in add mode
If(!$handle = fopen($filename,"a")){
echo "File cannot be opened";
exit();
            }
                 fclose($handle);
               }else{
echo "File $filename is not writable";
            }
}
}


/**
* Get the table name in the database
* return $str Loops to generate SQL statements for creating database tables and inserting 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 the table structure and values ​​in the database
* return $tabledump returns the sql statement of the structure and value in 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;
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632928.htmlTechArticlephp backup mysql database is a function that many personal webmaster websites have. Of course, if you want to back up several GB or When the database is larger, the php backup method feels a bit difficult. PHP backup mys...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template