Skip to content

Commit 20a6cc7

Browse files
committed
Added download_db.php script.
1 parent d9d57c3 commit 20a6cc7

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

download_db.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
$db_host = 'localhost';
4+
$db_user = 'username';
5+
$db_pass = 'password';
6+
$db_name = 'database';
7+
8+
$db = mysql_connect($db_host, $db_user, $db_pass);
9+
mysql_select_db($db_name, $db);
10+
11+
$sql = '';
12+
$tables = mysql_query('SHOW TABLES');
13+
14+
while ($t = mysql_fetch_row($tables))
15+
{
16+
$t = $t[0];
17+
18+
$create_table = mysql_fetch_array(mysql_query("SHOW CREATE TABLE `{$t}`"));
19+
$sql .= "\nDROP TABLE IF EXISTS `{$t}`;\n";
20+
$sql .= "{$create_table['Create Table']};\n\n";
21+
22+
$rows = mysql_query("SELECT * FROM `{$t}`");
23+
24+
while ($row = mysql_fetch_assoc($rows))
25+
{
26+
$sql .= "INSERT INTO `{$t}` SET";
27+
28+
foreach ($row as $key => $value)
29+
{
30+
$sql .= " `{$key}` = '" . mysql_escape_string($value) . "',";
31+
}
32+
33+
$sql = rtrim($sql, ',') . ";\n";
34+
}
35+
}
36+
37+
mysql_close($db);
38+
39+
header("Content-disposition: attachment; filename={$db_name}.sql");
40+
header('Content-type: text/sql');
41+
42+
echo $sql;

0 commit comments

Comments
 (0)