Thinkphp How to back up a database share _php tutorial

Source: Internet
Author: User

Thinkphp Backup Database method sharing


This article mainly introduces the method sharing of thinkphp backup database, it is very simple and practical, it is recommended to the small partners who need it.

Seemingly thinkphp no backup database method, so I wrote a, database connection and transaction processing with PDO, if necessary can contact me, write a mysql or mysqli

The code is as follows:


Class Sqlaction extends action{
function Outsql () {
Header ("Content-type:text/html;charset=utf-8″");
/* Read the database configuration using the C method */
$host =c (' db_host ');
$db _name=c (' db_name ');
$user =c (' Db_user ');
$password =c (' db_pwd ');
/* Call the private method of the export database */
$outstream = $this->outputsql ($host, $dbname, $user, $password);
/* Download the Export database */
Header ("content-disposition:attachment; Filename= $dbname. sql ");
Echo $outstream;
}
/*
* Database Export function Outputsql
* Export database data in PDO mode
* $host host name such as localhost
* $dbname Database name
* $user User Name
* $password Password
* $flag Flag bit 0 or 1 0 for export only database structure 1 for export database structure and data default to 1
*/
Private Function Outputsql ($host, $dbname, $user, $password, $flag =1) {
try {
$pdo = new PDO ("mysql:host= $host;d bname= $dbname", $user, $password); Connecting to a database
$pdo->setattribute (Pdo::attr_errmode, pdo::errmode_exception); Setting tuning parameters, encountering errors throwing exceptions
} catch (Pdoexception $e) {
echo $e->getmessage (); Throws an error message if a connection exception occurs
Exit
}
$mysql = "DROP DATABASE IF EXISTS ' $dbname '; \ n"; $MYSQL load the SQL statement, drop the database if there is a database
$creat _db= $pdo->query ("Show Create Database $dbname")->fetch ();//view SQL statements with show CREATE database
Preg_match ('/default CHARACTER set (. *) \*/', $creat _db[' Create Database '], $matches);//Regular Remove the character set after the DEFAULT CHARACTER set
$mysql. = "CREATE Database $dbname ' default CHARACTER SET $matches [1]";//the statement such as CREATE database ' test_db ' default CHARACTER S ET UTF8
/* Find the character sequence for the database, such as collate utf8_general_ci*/
$db _collate= $pdo->query ("Select Default_collation_name from INFORMATION_SCHEMA. schemata WHERE schema_name = ' $dbname ' LIMIT 1″)->fetch ();
$mysql. = "COLLATE". $db _collate[' Default_collation_name ']. "; Nuse ' $dbname '; \ n ';
$statments = $pdo->query ("Show Tables"); Return result set, Show Tables view all table names
foreach ($statments as $value) {//Traverse this result set to export information for each table name
$table _name = $value [0]; Get the table name
$mysql. = "DROP TABLE IF EXISTS ' $table _name '; \ n"; Prepare drop statements before each table
$table _query = $pdo->query ("Show create TABLE ' $table _name '"); Take out the result set of the table-building information
$create _sql = $table _query->fetch (); Use the Fetch method to remove the corresponding array of the result set
$mysql. = $create _sql[' Create Table ']. "; \r\n\r\n"; Write Build table information
if ($flag! = 0) {//If the flag bit is not 0 then continue to take out the table content generate INSERT statement
$iteams _query = $pdo->query ("select * from ' $table _name '"); Remove all field result sets for the table
$values = ""; Prepare an empty string to mount the Insert value value
$items = ""; Prepares an empty string to load the table field name
while ($item _query = $iteams _query->fetch (PDO::FETCH_ASSOC)) {//Returns an array of field names and values in the table using the associated query method
$item _names = Array_keys ($item _query); Remove the array key value that is the field name
$item _names = Array_map ("Addslashes", $item _names); Translate special characters to add \
$items = Join (' ', ' ', $item _names); Union field names such as: Items1 ', ' item2 ' symbol for back quote keyboard 1 next to field name in inverted quotation marks
$item _values = array_values ($item _query); Remove the value of the array value that is the field
$item _values = Array_map ("Addslashes", $item _values); Translate special characters to add \
$value _string = Join ("', '", $item _values); Union values such as: value1′, ' value2 values are enclosed in single quotes
$value _string = "('". $value _string. “‘),”; Parentheses on both sides of the value
$values. = "\ n". $value _string; And finally back to $value.
}
if ($values! = "") {//If $values is not empty, that table has content
Write INSERT statement
$insert _sql = "INSERT INTO ' $table _name ' (' $items ') VALUES". RTrim ($values, ","). "; \n\r";
Write the statement to $mysql
$mysql. = $insert _sql;
}
}

}
return $mysql;
}
}
?>

is not a very useful function, small partners can be directly transplanted into their own projects.

http://www.bkjia.com/PHPjc/975897.html www.bkjia.com true http://www.bkjia.com/PHPjc/975897.html techarticle thinkphp Backup Database method Sharing this article mainly introduces the thinkphp backup database method sharing, very simple and practical, recommended to the needs of the small partners like thinkphp not ...

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