<?php
/*
Download database. To invoke call:
download_mysql('DatabaseName','FileToStoreIn');
Must have the database connection running
*/
function download_mysql($DATABASE_NAME,$FNAME){
$ccyymmdd = date("Ymd");
$file = fopen("$FNAME.sql","w");
$line_count = 0;
$tables = mysql_list_tables($DATABASE_NAME);
$sql_string = NULL;
while ($table = mysql_fetch_array($tables)) {
$table_name = $table[0];
$table_query = mysql_query("SELECT * FROM `$table_name`");
$num_fields = mysql_num_fields($table_query);
while ($fetch_row = mysql_fetch_array($table_query)) {
$sql_string .= "INSERT INTO $table_name VALUES(";
$first = TRUE;
for ($field_count=1; $field_count<=$num_fields;$field_count++){
if (TRUE == $first) {
$sql_string .= "'".mysql_escape_string( $fetch_row[($field_count - 1)])."'";
$first = FALSE;
} else {
$sql_string .= ", '".mysql_escape_string( $fetch_row[($field_count - 1)])."'";
}
}
$sql_string .= ");\n";
if ($sql_string != ""){
fwrite($file, $sql_string);
++$line_count;
}
$sql_string = NULL;
}
fwrite($file, "\n\n");
}
fclose($file);
header("Cache-Control: public");
header("Content-Transfer-Encoding: binary\n");
header('Content-Type: application/text');
header("Content-Disposition: attachment; filename=\"$FNAME.sql\"");
header("Content-Length: ".filesize("$FNAME.sql"));
$file = fopen("$FNAME.sql",'r');
while($t = fread($file,102654)){
echo $t;
}
fclose($file);
$file = fopen("$FNAME.sql",'w');
fclose($file);
exit();
}
?>
|