Home > Backend Development > PHP Tutorial > PHP backup database class sharing_PHP tutorial

PHP backup database class sharing_PHP tutorial

WBOY
Release: 2016-07-13 09:57:24
Original
810 people have browsed it

PHP backup database class sharing

PHP backup database class sharing

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

/**

*

* @name php backup database

* @param string $DbHost connection host

* @param string $DbUser username

* @param string $DbPwd connection password

* @param string $DbName The database to be backed up

* @param string $saveFileName The name of the file to be saved. The default file is saved in the current folder, distinguished by date

* @return Null

* @example backupMySqlData('localhost', 'root', '123456', 'YourDbName');

*

*/

function backupMySqlData($DbHost, $DbUser, $DbPwd, $DbName, $saveFileName = '')

{

header("Content-type:text/html;charset=utf-8");

error_reporting(0);

set_time_limit(0);

echo 'Data is being backed up, please wait...
';

$link = mysql_connect($DbHost, $DbUser, $DbPwd) or die('Database connection failed: ' . mysql_error());

mysql_select_db($DbName) or die('Database connection failed: ' . mysql_error());

mysql_query('set names utf8');

//Declare variables

$isDropInfo = '';

$insertSQL = '';

$row = array();

$tables = array();

$tableStructure = array();

$fileName = ($saveFileName ? $saveFileName : 'MySQL_data_bakeup_') . date('YmdHis') . '.sql';

// Enumerate all tables in this database

$res = mysql_query("SHOW TABLES FROM $DbName");

while ($row = mysql_fetch_row($res)) {

$tables[] = $row[0];

}

mysql_free_result($res);

// Enumerate the creation statements of all tables

foreach ($tables as $val) {

$res = mysql_query("show create table $val", $link);

$row = mysql_fetch_row($res);

$isDropInfo = "DROP TABLE IF EXISTS `" . $val . "`;rn";

$tableStructure = $isDropInfo . $row[1] . ";rn";

file_put_contents($fileName, $tableStructure, FILE_APPEND);

mysql_free_result($res);

}

// Enumerate INSERT statements of all tables

foreach ($tables as $val) {

$res = mysql_query("select * from $val");

//Tables with no data will not perform insert

while ($row = mysql_fetch_row($res)) {

$sqlStr = "INSERT INTO `".$val."` VALUES (";

foreach($row as $v){

$sqlStr .= "'$v',";

}

//Remove the last comma

$sqlStr = substr($sqlStr, 0, strlen($sqlStr) - 1);

$sqlStr .= ");rn";

file_put_contents($fileName, $sqlStr, FILE_APPEND);

}

mysql_free_result($res);

}

echo 'Data backup successful! ';

}

// Call this method

backupMySqlData('localhost', 'root', '123456', 'YouDbName');

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/983323.htmlTechArticlephp backup database class sharing php backup database class sharing 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 4...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template