Using php to back up a mysql database is a feature provided by many personal webmaster websites. Of course, if you want to back up a few GB or larger databases, the php backup method is a bit difficult.
PHP backs up the source code of the mysql database. In the complete PHP + Mysql project, the background will have the function of backing up the Mysql database. With this function, we don't need to use FTP or mysql management tools to download mysql databases, which is very convenient. For phper that wants to do such a function, the principle is not very troublesome, there are three main points:
1. You must connect to the database to print out the mysql DATA table through SQL statements. 2. perform database operations through the PHP file operation function, including creating a folder for saving the mysql database, this step mainly involves creating a file. Step 3: Save the mysql database.
With this principle, we can create a backup database function of our own. below is the source code of a php database backup. The main structure is based on the above three points, the source code is composed of several methods. We can also encapsulate it into our own php class. I hope phper can design the mysql database backup function source code on this basis.
| The Code is as follows: |
Copy code |
<? Php /** Backup database generation. SQL File * @ Param $ browseinfo String browser version * Return $ browseinfo */ Function createsql (){ // Creation date $ Timer1 = time (); $ Path = "my_ SQL /"; $ Content = gettables (); $ Filename = $ path. $ timer1. ". SQL ";
// First, check if the folder is not in If (! File_exists ($ path )){ // If this directory does not exist, 0777 indicates the maximum read/write permission. 'If (mkdir ($ path, 0777 )){ // Echo "Creating a New directory "; } } // Determine whether a file exists If (! File_exists ($ filename )){ // If the object does not exist, create the object @ Fopen ($ filename, "w "); // Determine whether a file can be written If (is_writable ($ filename )){ // Open the file stream by adding the file as "" If (! $ Handle = fopen ($ filename, "")){ Echo "files cannot be opened "; Exit (); } If (! Fwrite ($ handle, $ content )){ Echo "files cannot be written "; Exit (); } // Close the file stream Fclose ($ handle ); Echo "generate a file and save the first content ";
} Else { Echo "File $ filename cannot be written "; } } Else { If (is_writable ($ filename )){ // Open the file stream by adding If (! $ Handle = fopen ($ filename, "")){ Echo "files cannot be opened "; Exit (); } Fclose ($ handle ); } Else { Echo "File $ filename cannot be written "; } } } /** * Obtain the table name in the database. * Return $ str generate SQL statements for database creation and insertion values cyclically */ Function gettables (){ $ Mysqli = new mysqli ("localhost", "root", "", "bbs "); $ Str = ''; If ($ result = $ mysqli-> query ("show tables ")){ While ($ row = $ result-> fetch_row ()){ $ Str. = data2sql ($ row [0]). "<br/> "; } $ Mysqli-> close (); Return $ str; }
}
/** * Obtain the table structure and value 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; } ?> |