php备份mysql数据库
Jun 23, 2016 pm 01:36 PM
<?php /*程序功能:mysql数据库备份功能*/ini_set('max_execution_time','0');ini_set('memory_limit','1024M');//加上这两行,可以备份大数据,昨晚测了一下公司的一个600M的表,很快就可以备份成功$code_type = 'gbk';header("Content-type:text/html;charset=$code_type");header("Content-disposition: filename=backup.sql");//所保存的文件名 header("Content-type: application/octetstream"); header("Pragma: no-cache"); header("Expires: 0"); $conn = mysql_connect("localhost","root","root"); $db = mysql_select_db($dbname,$conn); mysql_query("set names $code_type");$dbname="test";//数据库名$flag = '1';//1:全部备份,2:只备份表结构,3:只备份数据echo back_database($dbname,$conn,$flag);//备份数据库//开始备份function back_database($dbname,$conn,$flag='1'){$crlf="\r\n";$str = '';$now = date('Y年m月d日H点i分s秒',time());$str .= "$crlf--$crlf-- 数据库: `$dbname`$crlf--$crlf-- 导出日期: `$now`$crlf--$crlf";$str .= "$crlf--$crlf-- 作者: ****$crlf--$crlf-- QQ: 1019822077$crlf--$crlf";$str .= "-- blog: http://hi.baidu.com/woaidelphi/blog$crlf--$crlf";$str .= "SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";$crlf";$tables = mysql_list_tables($dbname,$conn); $num_tables = mysql_numrows($tables);//表的总数 $i = 0;while($i<$num_tables){//循环所有的表 $table = mysql_tablename($tables,$i); //备份表结构if($flag=='1' or $flag=='2'){$query = mysql_query("SHOW CREATE TABLE $table");$row = mysql_fetch_row($query);$str .= "-- --------------------------------------------------------$crlf$crlf";$str .= "--$crlf-- 表的结构 `$table` $crlf--$crlf";$str .= $row[1].";$crlf--$crlf";}//备份表内容 if($flag=='1' or $flag=='3'){$str .= "--$crlf-- 导出表中的数据 `$table` $crlf--$crlf";$str .= get_table_content($dbname,$table);$str .= "$crlf$crlf";}$i++; }return $str;}//得到表中的记录sql function get_table_content($dbname,$table){ $crlf = "\r\n";$schema_create = ""; $temp = ""; $result = mysql_db_query($dbname,"SELECT * FROM $table"); $i = 0; while($row = mysql_fetch_row($result)){ $schema_insert = "INSERT INTO $table VALUES ("; for($j=0; $j<mysql_num_fields($result);$j++){ if(!isset($row[$j])) $schema_insert .= " NULL,"; elseif($row[$j] != "") $schema_insert .= " '".addslashes($row[$j])."',"; else $schema_insert .= " '',"; } $schema_insert = ereg_replace(",$", "", $schema_insert); $schema_insert .= ");$crlf"; $temp = $temp.$schema_insert ; $i++; } return $temp; }
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?
3 weeks ago
By 王林
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?
3 weeks ago
By 王林
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon
