MySQL data backup class Code _php tutorial

Source: Internet
Author: User
Tags mysql tutorial
MySQL data backup class Code/*** * Description, this class for small Web site database backup, built-in MySQL connection, only need to simply configure the data connection * and storage backup location. * The Show_dir_file () method in the class can return all files in the backup directory directly, returning as an array

MySQL Tutorial data backup class Code
/***
* Description, this class is suitable for small Web site database tutorial backup, built-in MySQL connection, only need to simply configure the data connection
* and storage of backup location.
* The Show_dir_file () method in the class can return all files in the backup directory directly, returning as an array
* Method Expord_sql () generate SQL file directly
* This kind of production is simple, can be arbitrarily spread, how do you have any suggestions for this class, please send a message to shrimp
* Producer: Small shrimp on the day
* 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"; User name
Private $mysql _pwd = "lpl19881129"; Password
Private $mysql _db = "date"; Database name
Private $mysql _code = "GBK"; Encoding method

/***
* 1. Connect to the 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 statements
* **/
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 files that have been backed up in the storage directory
* **/
Public Function Show_dir_file () {
$dir = $this->data_dir;
if (!is_dir ($dir)) {
if (!mkdir ($dir)) {
echo "folder does not exist, try to create a folder, create a failure, may be you do not have the relevant permissions";
Exit ();
}else{
chmod ($dir, 755);
}
}

if (!is_writable ($dir)) {
echo "File is not writable";
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 is not writable";
Exit ();
}
Fwrite ($link, $tables);
Fclose ($link);
echo "Backup succeeded";
}

}

http://www.bkjia.com/PHPjc/630844.html www.bkjia.com true http://www.bkjia.com/PHPjc/630844.html techarticle MySQL Data backup class Code/*** * Description, this class for small Web site database backup, built-in MySQL connection, only need to simply configure the data connection * and storage backup location. ...

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