Też miałem taki problem. I pod swoje bazy poprawiłem tamten kod na coś takiego. Już nie pamiętam co tam poprawiłem, ale może będzie działać lepiej.
Tworzy plik do ściągnięcia. Nie zapisuje nigdzie tego na serwerze. Dodatkowo jako parametr table można podać nazwę tylko jednej tabeli do wygenerowania)
Częstuj się
<?php
$link = mysql_connect('127.0.0.1', 'root', 'haslo') or die('Connection error : ' . mysql_error());
mysql_select_db($MySQLDatabase) or die('Could not select database');
$sql = '';
$crlf = "\r\n";
$time = date('Y m d, H:i', time());
$sql .= '#' . $crlf;
$sql .= '# Zrzut bazy danych ' . $crlf;
$sql .= '# Wygenerowano: ' . $time . $crlf;
$sql .= '#' . $crlf . $crlf;
$res = mysql_list_tables($MySQLDatabase);
while($row = mysql_fetch_array($res))
{
$table_name = $row[0];
$table_fields = array();
if ((isset($table) and ($table_name == $table)) or (!isset($table)))
{
$sql .= '#' . $crlf;
$sql .= '# Struktura tabeli ' . $table_name . $crlf;
$sql .= '#' . $crlf;
$sql .= 'DROP TABLE IF EXISTS ' . $table_name . ';' . $crlf;
$sql .= 'CREATE TABLE ' . $table_name . ' ( ' . $crlf;
/* Generujemy strukture tabeli. Najpierw wyciagamy pola */
$res2 = mysql_query('SHOW FIELDS FROM ' . $table_name);
while($fields = mysql_fetch_array($res2))
{
$sql .= ' ';
$sql .= $fields['Field'] . ' ' . $fields['Type'];
if (!empty($fields['Default']))
{
$sql .= ' DEFAULT \'' . $fields['Default'] . '\'';
}
if ($fields['Null'] != 'Yes')
{
$sql .= ' NOT NULL';
}
if (!empty($fields['Extra']))
{
$sql .= ' ' . $fields['Extra'];
}
$sql .= ',' . $crlf;
$table_fields[] = $fields['Field'];
}
$index = '';
/* Teraz wyciagamy pola typu KEY */
$res2 = mysql_query('SHOW KEYS FROM ' . $table_name);
while ($keys = mysql_fetch_assoc($res2))
{
$kname = $keys['Key_name'];
if(($kname != 'PRIMARY') && ($keys['Non_unique'] == 0))
{
$kname = 'UNIQUE|' . $kname;
}
$index[$kname] = array();
$index[$kname][] = $keys['Column_name'];
}
$byl = 0;
while(list($n, $columns) = @each($index))
{
if ($n == 'PRIMARY')
{
if ($byl == 1)
$sql .= ",";
$byl = 1;
$sql .= ' PRIMARY KEY (' . implode($columns, ', ') . ')';
}
elseif (substr($n, 0, 6) == 'UNIQUE')
{
if ($byl == 1)
$sql .= ",";
$byl = 1;
$sql .= ' UNIQUE ' . substr($n, 7) . ' (' . implode($columns, ', ') . ')';
}
else
{
if ($byl == 1)
$sql .= ",";
$byl = 1;
$sql .= ' KEY ' . $n . ' (' . implode($columns, ', ') . ')';
}
$sql .= $crlf;
}
$sql .= ');' . $crlf;
$sql .= $crlf . $crlf;
$sql .= '#' . $crlf;
$sql .= '# Dane z tabeli ' . $table_name . $crlf;
$sql .= '#' . $crlf;
/* Mamy juz strukture, wiec teraz czas
wyciagnac dane z tabeli */
$d_res = mysql_query('SELECT * FROM ' . $table_name);
while ($data = mysql_fetch_array($d_res))
{
$sql .= 'INSERT INTO ' . $table_name . ' (' . implode(', ', $table_fields) . ') VALUES(';
$field_count = count($table_fields);
$f_data = array();
for ($i = 0; $i < $field_count; $i++)
{
$f_data[] .= '\'' . $data[$i] . '\'';
}
$sql .= implode(', ', $f_data);
$sql .= ');' . $crlf;
}
$sql .= $crlf . $crlf;
}
}
/* Zapis do pliku
$file = fopen('test.sql', 'w');
fwrite($file, $sql);
fclose($file);
*/
if (isset($table))
$file = "Gorion-SQL-".date("Y-m-d_H-i-s",time())."_".$table.".sql";
else
$file = "Gorion-SQL-".date("Y-m-d_H-i-s",time()).".sql";
header('Content-Type: application/x-unknown');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Content-Transfer-Encoding: binary');
// header('Content-Length: ' . sizeof($sql));
echo $sql;
?>