<?php
if(isset($_POST['submit'])) {
$tables = '*';
if($tables == '*') {
$tables = array();
$result = $con->query('SHOW TABLES');
while($row = $result->fetch_row())
{
//echo $row[0];
$tables[] = $row[0];
}
}else {
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
//cycle through
foreach($tables as $table)
{
$result = $con->query('SELECT * FROM '.$table);
$num_fields = $result->field_count;
$return.= 'DROP TABLE '.$table.';';
$row2 = $con->query('SHOW CREATE TABLE '.$table);
$row2 = $row2->fetch_row();
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = $result->fetch_row())
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
//save file
$handle = fopen('db/backup.sql','w+');
fwrite($handle,$return);
fclose($handle);
echo "<script>document.location.href='backup.php?msg=show'</script>";
}
?>
0 comments:
Post a Comment