php backup database, php backup_PHP tutorial
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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

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

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

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

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

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

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

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

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
