PHP MYSQL Data Backup class _php instance

Source: Internet
Author: User
Features are:
Require_once ("backdata.class.php");
$link = @mysql_connect ("localhost", "Database name", "password") or Die (' could not connect to server. ');
mysql_query ("Use CMS", $link);
mysql_query ("Set names UTF8", $link);
$dbbck =new Backupdata ($link);//Instantiate it, as long as a link identity on the line
When backing up your data, if you want to back up all the tables in a database, you can write this:
$dbbck->backuptables ("CMS", "./", Array (' * '));
­
When backing up your data, if you want to back up only one table in a database, you can write this:
$dbbck->backuptables ("CMS", "./", Array (' user '));
­
When backing up your data, if you want to back up multiple tables in a database, you can write this:
­
$dbbck->backuptables ("CMS", "./", Array (' user ', ' ACL ', ' informatoin '));
Note: $dbbck->backuptables ("Parameter 1", "Parameter 2", Array ());
Parameter 1 is: database name,
Parameter 2 is the location where the backup data is to be stored (that is, the directory address)
The third one is: you have to save those tables.
Ok...
­
The following is code:
Copy Code code as follows:

<?php
/*
*
* A simple backup data class
*author FC
*
*/
Class backupdata{
Private $mysql _link;//Link identification
private $dbName;//Database name
Private $dataDir; The directory where the data is to be stored
private $tableNames;//Table name
Public function __construct ($mysql _link) {
­
$this->mysql_link = $mysql _link;
}
­
Public Function Backuptables ($dbName, $dataDir, $tableNames) {//Start Backup
­
$this->dbname = $dbName;
$this->datadir = $dataDir;
$this->tablenames = $tableNames;
$tables = $this->delarray ($this->tablenames);
$sqls = ';
foreach ($tables as $tablename) {
if ($tablename = =) {//table does not exist
Continue
}
Here is the first half of the form SQL **************
If there is a table, delete it first
$sqls. = "DROP TABLE IF EXISTS $tablename; \ n";
Reading table structure
$rs = mysql_query ("Show CREATE TABLE $tablename", $this->mysql_link);
$row =mysql_fetch_row ($RS);
Get table structure to compose SQL
$sqls. = $row [' 1 ']. "; \ N\n ";
Unset ($RS);
Unset ($row);
The following is the second half of the form SQL **************
Search out all the data in the table
$rs =mysql_query ("SELECT * from $tablename", $this->mysql_link);
Number of fields in a table
$field =mysql_num_fields ($RS);
form This SQL statement: INSERT into ' groups ' VALUES (' 1499e0ca25988d ', ' director ', ', ', ' 0 '); "
while ($rows =mysql_fetch_row ($rs)) {
$comma = ';/comma
$sqls. = "INSERT into ' $tablename ' VALUES (";
for ($i =0; $i < $field; $i + +) {
$sqls. = $comma. "'". $rows [$i]. "'";
$comma = ', ';
}
$sqls. = "); \n\n\n";
}
}
$backfilepath = $this->datadir.date ("Ymdhis", Time ()). SQL ';
Write to File
$filehandle = fopen ($backfilepath, "w");
Fwrite ($filehandle, $SQLS);
Fclose ($filehandle);
}
­
Private Function Delarray ($array) {//Processing incoming array
foreach ($array as $tables) {
if ($tables = = ' * ') {//All tables (the table name cannot be grouped into an array in the usual way)
$newtables =mysql_list_tables ($this->dbname, $this->mysql_link);
$tableList = Array ();
for ($i = 0; $i < mysql_numrows ($newtables); $i + +) {
Array_push ($tableList, Mysql_tablename ($newtables, $i));
}
$tableList = $tableList;
}else{
$tableList = $array;
Break
}
}
return $tableList;
}
}

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.