Maison > développement back-end > tutoriel php > php备份数据库,php备份_PHP教程

php备份数据库,php备份_PHP教程

WBOY
Libérer: 2016-07-13 09:57:29
original
805 Les gens l'ont consulté

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>
Copier après la connexion

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/981700.htmlTechArticlephp备份数据库,php备份 ? php /* * * * @name php备份数据库 * @param string $DbHost 连接主机 * @param string $DbUser 用户名 * @param string $DbPwd 连接密码 *...
Étiquettes associées:
php
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal