Table of Contents
php backup database, php backup
Home Backend Development PHP Tutorial php backup database, php backup_PHP tutorial

php backup database, php backup_PHP tutorial

Jul 13, 2016 am 09:57 AM
php

php backup database, php backup

<span><?<span>php
    </span><span>/*</span><span>*
    *    
    *    @name  php备份数据库 
    *    @param string   $DbHost       连接主机
    *    @param string   $DbUser       用户名
    *    @param string   $DbPwd        连接密码
    *    @param string   $DbName       要备份的数据库
    *    @param string   $saveFileName 要保存的文件名, 默认文件保存在当前文件夹中,以日期作区分
    *    @return Null
    *    @example backupMySqlData('localhost', 'root', '123456', 'YourDbName');
    *
    </span><span>*/</span>
    <span>function</span> backupMySqlData(<span>$DbHost</span>, <span>$DbUser</span>, <span>$DbPwd</span>, <span>$DbName</span>, <span>$saveFileName</span> = ''<span>)
    {
        </span><span>header</span>("Content-type:text/html;charset=utf-8"<span>);
        </span><span>error_reporting</span>(0<span>);
        </span><span>set_time_limit</span>(0<span>);

        </span><span>echo</span> '数据备份中,请稍候......<br />'<span>;

        </span><span>$link</span> = <span>mysql_connect</span>(<span>$DbHost</span>, <span>$DbUser</span>, <span>$DbPwd</span>) or <span>die</span>('数据库连接失败: ' . <span>mysql_error</span><span>());
        </span><span>mysql_select_db</span>(<span>$DbName</span>) or <span>die</span>('数据库连接失败: ' . <span>mysql_error</span><span>());
        </span><span>mysql_query</span>('set names utf8'<span>);
        
        </span><span>//</span><span> 声明变量</span>
        <span>$isDropInfo</span>     = ''<span>;
        </span><span>$insertSQL</span>      = ''<span>;
        </span><span>$row</span>            = <span>array</span><span>();
        </span><span>$tables</span>         = <span>array</span><span>();
        </span><span>$tableStructure</span> = <span>array</span><span>();
        </span><span>$fileName</span>       = (<span>$saveFileName</span> ? <span>$saveFileName</span> : 'MySQL_data_bakeup_') . <span>date</span>('YmdHis') . '.sql'<span>;

        </span><span>//</span><span> 枚举该数据库所有的表</span>
        <span>$res</span> = <span>mysql_query</span>("SHOW TABLES FROM <span>$DbName</span>"<span>);
        </span><span>while</span> (<span>$row</span> = <span>mysql_fetch_row</span>(<span>$res</span><span>)) {

            </span><span>$tables</span>[] = <span>$row</span>[0<span>];

        }
        </span><span>mysql_free_result</span>(<span>$res</span><span>);

        </span><span>//</span><span> 枚举所有表的创建语句</span>
        <span>foreach</span> (<span>$tables</span> <span>as</span> <span>$val</span><span>) {

            </span><span>$res</span> = <span>mysql_query</span>("show create table <span>$val</span>", <span>$link</span><span>);
            </span><span>$row</span> = <span>mysql_fetch_row</span>(<span>$res</span><span>);

            </span><span>$isDropInfo</span>     = "DROP TABLE IF EXISTS `" . <span>$val</span> . "`;\r\n"<span>;
            </span><span>$tableStructure</span> = <span>$isDropInfo</span> . <span>$row</span>[1] . ";\r\n"<span>;

            </span><span>file_put_contents</span>(<span>$fileName</span>, <span>$tableStructure</span>,<span> FILE_APPEND);
            </span><span>mysql_free_result</span>(<span>$res</span><span>);
        }

        </span><span>//</span><span> 枚举所有表的INSERT语句</span>
        <span>foreach</span> (<span>$tables</span> <span>as</span> <span>$val</span><span>) {

            </span><span>$res</span> = <span>mysql_query</span>("select * from <span>$val</span>"<span>);

            </span><span>//</span><span> 没有数据的表不执行insert</span>
            <span>while</span> (<span>$row</span> = <span>mysql_fetch_row</span>(<span>$res</span><span>)) {

                </span><span>$sqlStr</span> = "INSERT INTO `".<span>$val</span>."` VALUES ("<span>;

                </span><span>foreach</span>(<span>$row</span> <span>as</span> <span>$v</span><span>){

                    </span><span>$sqlStr</span> .= "'<span>$v</span>',"<span>;

                }
                </span><span>//</span><span>去掉最后一个逗号</span>
                <span>$sqlStr</span>  = <span>substr</span>(<span>$sqlStr</span>, 0, <span>strlen</span>(<span>$sqlStr</span>) - 1<span>);
                </span><span>$sqlStr</span> .= ");\r\n"<span>;

                </span><span>file_put_contents</span>(<span>$fileName</span>, <span>$sqlStr</span>,<span> FILE_APPEND);
            }
            </span><span>mysql_free_result</span>(<span>$res</span><span>);
        }

        </span><span>echo</span> '数据备份成功!'<span>;
    }
   // 调用此方法 
    backupMySqlData(</span>'localhost', 'root', '123456', 'YouDbName'<span>);    
</span>?></span>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/981700.htmlTechArticlephp backup database, php backup? php /* * * * @name php backup database* @param string $DbHost Connect to host* @param string $DbUser user name* @param string $DbPwd Connect password*...
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles