Home  >  Article  >  Backend Development  >  PHP backup database generates SQL file and downloads function code_PHP tutorial

PHP backup database generates SQL file and downloads function code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:20:59894browse

Copy code The code is as follows:

/****** Back up database structure ******/
/*
Function name: table2sql()
Function function: Convert the table structure into SQL
Function parameter: $table: the name of the table to be extracted
Return value: Return the extracted result, SQL Set
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;
}
/****** Back up the database structure and all data ******/
/*
Function name: data2sql()
Function function : Convert the table structure and data into SQL
function parameters: $table: table name to be extracted
return value: return the extracted results, 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;
}
?>