<?php
Song Jong Reprint Please indicate the source
Set_time_limit (0);
Header (' Content-type:text/html;charset=utf-8 ');
mysql_connect (' localhost ', ' root ', ' root ');
mysql_select_db (' Test ');
$table _array=get_tables (' Test ');
mysql_query (' Set names UTF8 ');
$filesize =1024*1024*4;
$start =$_get[' start ']?$_get[' Start ']:0;
$part =$_get[' part ']?$_get[' part ': ' 1 ';
$table _index=$_get[' table_index ']?$_get[' Table_index ']: ' 0 ';
$table = $table _array[$table _index];
$num =200000000;//This number to be large enough to be the total number of records
$backupdata = ";
if ($start = = ' 0 ') {
$query = "SHOW CREATE TABLE ' {$table} '";
$result = mysql_query ($query);
$row = Mysql_fetch_row ($result);
$backupdata. = "DROP TABLE IF EXISTS ' {$table} '; \ n". $row [1]. "; \ n";
}
$limit = ($start = = ' 0 ')? ":" Limit $start, $num ";
$query = "Select * from ' {$table} ' $limit";
$result =mysql_query ($query);
$numfields = Mysql_num_fields ($result); Number of statistics fields
while ($row =mysql_fetch_row ($result)) {
$comma = "; Storing commas
$backupdata _tmp = "INSERT into ' {$table} ' VALUES (";
for ($i =0; $i < $numfields; $i + +) {
$backupdata _tmp. = $comma. "‘" . Mysql_escape_string ($row [$i]). "‘";
$comma = ', ';
}
$backupdata _tmp. = "); \ n";
if (strlen ($backupdata) +strlen ($backupdata _tmp) > $filesize) {
Write to file and jump
$file = ' data/'. $table. ' -'. $part. ' SQL ';
File_put_contents ($file, $backupdata);
echo $file. ' Backup complete, the program continues! ‘;
$part + +;
Segmentation
Table name
Starting point
Jump
Sleep (3);
echo "<script>location.href="? start={$start}&table_index={$table _index}&part={$part} '; </script > ";
Exit
}
$backupdata. = $backupdata _tmp;
$start + +;
}
if ($backupdata) {
$file = ' data/'. $table. ' -'. $part. ' SQL ';
File_put_contents ($file, $backupdata);
}
echo $table. ' Backup Complete! <br/> ';
Sleep (2);
$table _index++;
if ($table _array[$table _index]) {
echo "<script>location.href="? table_index={$table _index} ';</script> ';
Exit
}else{
Echo ' Congratulations, database backup is complete! ‘;
}
function Get_tables ($db) {
$TQ = Mysql_list_tables ($db);
while ($tr = Mysql_fetch_row ($TQ)) {
$ARRTB [] = $tr [0];
}
return $ARRTB;
}
?>
Php+mysql Database sub-table segmented Backup program--Song Jong