Home >Backend Development >PHP Tutorial >mysql data backup code_PHP tutorial
mysql data backup code /*** * Description: This type is suitable for database backup of small websites. It has built-in MYSQL connection and only requires simple configuration of data connection. * and the location where the backup is stored. * The show_dir_file() method in the class can directly return all files in the backup directory in the form of an array
mysql tutorial data backup code
/***
* Note, this type of database tutorial backup is suitable for small websites. It has built-in mysql connection and only requires simple configuration of data connection
* and the location where the backup is stored.
* The show_dir_file() method in the class can directly return all files in the backup directory in the form of an array
* Method expord_sql() directly generates sql file
* This type is simple to make and can be spread at will. If you have any suggestions for this type, please send an email to Xiaoxia
* Producer: Youtian Xiaoxia
* emial:328742379@qq.com
* **/
class data {
public $data_dir = "class/"; //The path where the backup file is stored
public $data_name = "111cnnet.sql"; //Backup file name
private $mysql_host = "localhost"; //Database address
private $mysql_user = "root"; //Username
private $mysql_pwd = "lpl19881129"; //Password
private $mysql_db = "date"; //Database name
private $mysql_code = "gbk"; //Encoding method
/***
* 1. Connect to database
* **/
function __construct(){
$conn = mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_pwd);
mysql_select_db($this->mysql_db);
mysql_query("set names $this->mysql_code");
}
/***
* 2. Generate sql statement
* **/
private function set_sql($table){
$tabledump = "drop table if exists $table;n";
$createtable = mysql_query("show create table $table");
$create = mysql_fetch_row($createtable);
$tabledump .= $create[1].";nn";
$rows = mysql_query("select * from $table");
$numfields = mysql_num_fields($rows);
$numrows = mysql_num_rows($rows);
while ($row = mysql_fetch_row($rows)){
$comma = "";
$tabledump .= "insert into $table values(";
for($i = 0; $i < $numfields; $i++)
{
$tabledump .= $comma."'".mysql_escape_string($row[$i])."'";
$comma = ",";
}
$tabledump .= ");n";
}
$tabledump .= "n";
return $tabledump;
}
/***
* 3. Display the backed up files in the storage directory
* **/
Public function show_dir_file() {
$dir = $this->data_dir;
If(!is_dir($dir)){
if(!mkdir($dir)){
echo "The folder does not exist. Try to create the folder, but the creation fails. It may be that you do not have the relevant permissions";
exit();
}else{
chmod($dir,755);
}
}
if(!is_writable($dir)){
echo "File cannot be written";
exit();
}
$link = opendir($dir);
if(!$link){
echo "Failed to create link";
exit();
}
return scandir($dir);
}
/***
* 4. Generate sql file
* **/
Public function expord_sql(){
$this->show_dir_file();
$result = mysql_list_tables($this->mysql_db);
while($arr = mysql_fetch_row($result)){
$tables .= $this->set_sql($arr[0]);
}
$file = $this->data_dir.$this->data_name;
$link = fopen($file,"w+");
if(!is_writable($file)){
echo "File not writable";
exit();
}
fwrite($link,$tables);
fclose($link);
echo "Backup successful";
}
}