· 8 years ago · Apr 23, 2018, 12:24 PM
1function mysql_dump($database) {
2
3 $query = '';
4
5 $tables = @mysql_list_tables($database);
6 while ($row = @mysql_fetch_row($tables)) { $table_list[] = $row[0]; }
7
8 for ($i = 0; $i < @count($table_list); $i++) {
9
10 $results = mysql_query('DESCRIBE ' . $database . '.' . $table_list[$i]);
11
12 $query .= 'DROP TABLE IF EXISTS `' . $database . '.' . $table_list[$i] . '`;' . lnbr;
13 $query .= lnbr . 'CREATE TABLE `' . $database . '.' . $table_list[$i] . '` (' . lnbr;
14
15 $tmp = '';
16
17 while ($row = @mysql_fetch_assoc($results)) {
18
19 $query .= '`' . $row['Field'] . '` ' . $row['Type'];
20
21 if ($row['Null'] != 'YES') { $query .= ' NOT NULL'; }
22 if ($row['Default'] != '') { $query .= ' DEFAULT \'' . $row['Default'] . '\''; }
23 if ($row['Extra']) { $query .= ' ' . strtoupper($row['Extra']); }
24 if ($row['Key'] == 'PRI') { $tmp = 'primary key(' . $row['Field'] . ')'; }
25
26 $query .= ','. lnbr;
27
28 }
29
30 $query .= $tmp . lnbr . ');' . str_repeat(lnbr, 2);
31
32 $results = mysql_query('SELECT * FROM ' . $database . '.' . $table_list[$i]);
33
34 while ($row = @mysql_fetch_assoc($results)) {
35
36 $query .= 'INSERT INTO `' . $database . '.' . $table_list[$i] .'` (';
37
38 $data = Array();
39
40 while (list($key, $value) = @each($row)) { $data['keys'][] = $key; $data['values'][] = addslashes($value); }
41
42 $query .= join($data['keys'], ', ') . ')' . lnbr . 'VALUES (\'' . join($data['values'], '\', \'') . '\');' . lnbr;
43
44 }
45
46 $query .= str_repeat(lnbr, 2);
47
48 }
49
50 return $query;
51
52 }