· 7 years ago · Jan 17, 2019, 05:46 AM
1// Connect to database
2 $db = mysql_select_db($this->DATABASE_DB);
3
4 if (!$db) {
5 die ('Can't use foo : ' . mysql_error());
6 }
7 if (!empty($db)) {
8
9
10 // Get all table names from database
11 $c = 0;
12 $result = mysql_list_tables($this->DATABASE_DB);
13 for($x = 0; $x < mysql_num_rows($result); $x++) {
14 $table = mysql_tablename($result, $x);
15 if (!empty($table)) {
16 $arr_tables[$c] = mysql_tablename($result, $x);
17 $c++;
18 }
19 }
20
21
22 // Structure Header
23 $structure .= "-- n";
24 $structure .= "-- Backup effettuato il `".date('Y-m-d H:i:s')."` n";
25 $structure .= "-- Backup per database `{$this->DATABASE_DB}` n";
26 $structure .= "-- nn";
27
28 $structure .= "-- nn";
29 $structure .= "-- nn";
30
31 $structure .= "SET NAMES utf8; n";
32 $structure .= "SET FOREIGN_KEY_CHECKS = 0;";
33
34
35 $structure .= "-- nn";
36 $structure .= "-- nn";
37
38 // List tables
39 $dump = '';
40 for ($y = 0; $y < count($arr_tables); $y++){
41
42 // DB Table name
43 $table = $arr_tables[$y];
44
45 // Structure Header
46 $structure .= "-- n";
47 $structure .= "-- Table structure for table `{$table}` n";
48 $structure .= "-- nn";
49
50 // Dump Structure
51 $structure .= "DROP TABLE IF EXISTS `{$table}`; n";
52 $structure .= "CREATE TABLE `{$table}` (n";
53 $result = mysql_db_query($this->DATABASE_DB, "SHOW FIELDS FROM `{$table}`");
54 while($row = mysql_fetch_object($result)) {
55
56 $structure .= " `{$row->Field}` {$row->Type}";
57 $structure .= (!empty($row->Default)) ? " DEFAULT `{$row->Default}`" : false;
58 $structure .= ($row->Null != "YES") ? " NOT NULL" : false;
59 $structure .= (!empty($row->Extra)) ? strtoupper(" {$row->Extra}") : false;
60 $structure .= ",n";
61
62 }
63
64 $structure = ereg_replace(",n$", "", $structure);
65
66 // Save all Column Indexes in array
67 unset($index);
68 $result = mysql_db_query($this->DATABASE_DB, "SHOW KEYS FROM `{$table}`");
69 while($row = mysql_fetch_object($result)) {
70
71 if (($row->Key_name == 'PRIMARY') AND ($row->Index_type == 'BTREE')) {
72 $index['PRIMARY'][$row->Key_name] = $row->Column_name;
73 }
74
75 if (($row->Key_name != 'PRIMARY') AND ($row->Non_unique == '0') AND ($row->Index_type == 'BTREE')) {
76 $index['UNIQUE'][$row->Key_name] = $row->Column_name;
77 }
78
79 if (($row->Key_name != 'PRIMARY') AND ($row->Non_unique == '1') AND ($row->Index_type == 'BTREE')) {
80 $index['INDEX'][$row->Key_name] = $row->Column_name;
81 }
82
83 if (($row->Key_name != 'PRIMARY') AND ($row->Non_unique == '1') AND ($row->Index_type == 'FULLTEXT')) {
84 $index['FULLTEXT'][$row->Key_name] = $row->Column_name;
85 }
86
87 }
88
89 // Return all Column Indexes of array
90 if (is_array($index)) {
91 foreach ($index as $xy => $columns) {
92
93 $structure .= ",n";
94
95 $c = 0;
96 foreach ($columns as $column_key => $column_name) {
97
98 $c++;
99
100 $structure .= ($xy == "PRIMARY") ? " PRIMARY KEY (`{$column_name}`)" : false;
101 $structure .= ($xy == "UNIQUE") ? " UNIQUE KEY `{$column_key}` (`{$column_name}`)" : false;
102 $structure .= ($xy == "INDEX") ? " KEY `{$column_key}` (`{$column_name}`)" : false;
103 $structure .= ($xy == "FULLTEXT") ? " FULLTEXT `{$column_key}` (`{$column_name}`)" : false;
104
105 $structure .= ($c < (count($index[$xy]))) ? ",n" : false;
106
107 }
108
109 }
110
111 }
112
113 $structure .= "n);nn";
114
115 // Header
116 $structure .= "-- n";
117 $structure .= "-- Dumping data for table `$table` n";
118 $structure .= "-- nn";
119
120 // Dump data
121 unset($data);
122
123
124 //mysql_set_charset('latin1');
125 $result = mysql_query("SELECT * FROM `$table`");
126 $num_rows = mysql_num_rows($result);
127 $num_fields = mysql_num_fields($result);
128
129 for ($i = 0; $i < $num_rows; $i++) {
130
131 $row = mysql_fetch_object($result);
132 $data .= "INSERT INTO `$table` (";
133
134 // Field names
135 for ($x = 0; $x < $num_fields; $x++) {
136
137 $field_name = mysql_field_name($result, $x);
138
139 $data .= "`{$field_name}`";
140 $data .= ($x < ($num_fields - 1)) ? ", " : false;
141
142 }
143
144 $data .= ") VALUES (";
145
146 // Values
147 for ($x = 0; $x < $num_fields; $x++) {
148 $field_name = mysql_field_name($result, $x);
149
150 $data .= "'" . str_replace('"', '"', mysql_escape_string($row->$field_name)) . "'";
151 $data .= ($x < ($num_fields - 1)) ? ", " : false;
152
153 }
154
155 $data.= ");n";
156 }
157
158 $data.= "n";
159
160 $dump .= $structure . $data;
161 $dump .= "-- --------------------------------------------------------nn";
162
163 }
164
165 return $dump;
166
167 }
168
169}
170
171SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8';