Home > php教程 > php手册 > php备份数据库,php备份

php备份数据库,php备份

WBOY
Release: 2016-06-13 09:07:21
Original
894 people have browsed it

php备份数据库,php备份

<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

 

Related labels:
php
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template