Home  >  Article  >  Backend Development  >  sql file PHP backup database generates SQL file and downloads the function code

sql file PHP backup database generates SQL file and downloads the function code

WBOY
WBOYOriginal
2016-07-29 08:47:581035browse

Copy code The code is as follows:


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