Copy Code code as follows:
<!? Php
/****** BACKUP DATABASE Structure ******/
/*
Function name: Table2sql ()
function function: Transform the structure of a table into SQL
Function parameters: $table: Table name to be extracted
Return value: Returns the result after extraction, SQL collection
Function Author: heiyeluren
*/
function Table2sql ($table)
{
Global $db;
$tabledump = "DROP TABLE IF EXISTS $table; n";
$createtable = $db--->query ("show CREATE TABLE $table");
$create = $db->fetch_row ($createtable);
$tabledump. = $create [1]. "; nn ";
return $tabledump;
}
/****** backup database structure and all data ******/
/*
Function name: Data2sql ()
function function: Transform the structure and data of a table into SQL
Function parameters: $table: Table name to be extracted
Return value: Returns the result after extraction, SQL collection
Function Author: heiyeluren
*/
function Data2sql ($table)
{
Global $db;
$tabledump = "DROP TABLE IF EXISTS $table; n";
$createtable = $db->query ("Show CREATE TABLE $table");
$create = $db->fetch_row ($createtable);
$tabledump. = $create [1]. "; nn ";
$rows = $db->query ("SELECT * from $table");
$numfields = $db->num_fields ($rows);
$numrows = $db->num_rows ($rows);
while ($row = $db->fetch_row ($rows))
{
$comma = "";
$tabledump. = "INSERT into $table VALUES (";
for ($i = 0; $i < $numfields; $i + +)
{
$tabledump. = $comma. "'". Mysql_escape_string ($row [$i]). "'";
$comma = ",";
}
$tabledump. = "); n";
}
$tabledump. = "n";
return $tabledump;
}
?>
<!--? php
$host = "localhost"; Host Name
$user = "root"; MySQL User name
$password = "root"; Password
$dbname = "DEDECMSV4"; Backed up databases
Mysql_connect ($host, $user, $password);
mysql_select_db ($dbname);
$q 1=mysql_query ("Show Tables");
while ($t =mysql_fetch_array ($q 1)) {
$table = $t [0];
$q 2=mysql_query ("Show create Table ' $table '");
$sql =mysql_fetch_array ($q 2);
$mysql. = $sql [' Create Table ']. " Rnrn "; #DDL
$q 3=mysql_query ("select * from ' $table");
while ($data =mysql_fetch_assoc ($q 3))
{
$keys =array_keys ($data);
$keys =array_map (' addslashes ', $keys);
$keys =join (', ', $keys);
$keys = "'. $keys." `";
$vals =array_values ($data);
$vals =array_map (' addslashes ', $vals);
$vals =join ("', '", $vals);
$vals = "'" $vals. "'";
$mysql. = "INSERT INTO ' $table ' ($keys) values ($vals); RN";
}
$mysql. = "RN";
}
$filename =date (' Ymd '). " _ ". $dbname.". SQL "; File name is the date of the day
$fp = fopen ($filename, ' w ');
Fputs ($fp, $mysql);
Fclose ($FP);
echo "Data backup succeeded, generate backup file". $filename;
?>