How to use PHP pages to back up and restore the data tables in the MySQL database
Last Update:2017-02-28
Source: Internet
Author: User
mysql| Backup | recovery | data | database | page
Backing up data
$i = 0;
$crlf = "\ r \ n";
$dbname = "XGTQR";
Global $dbconn;
$dbconn = mysql_connect ("localhost", "root", "root");
$db = mysql_select_db ("XGTQR", $dbconn);
$tables = Mysql_list_tables ("XGTQR", $dbconn);
$num _tables = @mysql_numrows ($tables);
while ($i < $num _tables)
{
$table = Mysql_tablename ($tables, $i);
Print $crlf;
Print $crlf;
Echo get_table_def ($dbname, $table, $crlf). "; $crlf $crlf ";
Echo get_table_content ($dbname, $table, $CRLF);
$i + +;
}
Define the extraction table structure and data
function Get_table_def ($db, $table, $crlf)
{
Global $drop;
$schema _create = "";
if (!empty ($drop))
$schema _create. = "DROP TABLE IF EXISTS $table; $crlf";
$schema _create. = "Create TABLE $table ($CRLF);
$result = Mysql_db_query ($db, "show FIELDS from $table");
while ($row = Mysql_fetch_array ($result))
{
$schema _create. = "$row [Field] $row [Type]";
if (Isset ($row ["Default"]) && (!empty ($row ["Default"]) | | $row ["Default"] = = "0"))
$schema _create. = "Default" $row [Default] ";
if ($row ["Null"]!= "YES")
$schema _create. = "Not NULL";
if ($row ["Extra"]!= "")
$schema _create. = "$row [Extra]";
$schema _create. = ", $crlf";
}
$schema _create = ereg_replace (",". $crlf. " $ "," ", $schema _create);
$result = Mysql_db_query ($db, "show the KEYS from $table");
while ($row = Mysql_fetch_array ($result))
{
$kname = $row [' Key_name '];
if ($kname!= "PRIMARY") && ($row [' non_unique '] = = 0))
$kname = "unique| $kname";
if (!isset ($index [$kname]))
$index [$kname] = array ();
$index [$kname] = $row [' column_name '];
}
while (list ($x, $columns) = @each ($index))
{
$schema _create. = ", $crlf";
if ($x = = "PRIMARY")
$schema _create. = "PRIMARY KEY". Implode ($columns, ","). ")";
ElseIf (substr ($x, 0,6) = = "UNIQUE")
$schema _create. = "UNIQUE". substr ($x, 7). " (" . Implode ($columns, ","). ")";
Else
$schema _create. = "KEY $x (". Implode ($columns, ","). ")";
}
$schema _create. = "$crlf)";
Return (Stripslashes ($schema _create));
}
Save the value of a data table
function Get_table_content ($db, $table, $crlf)
{
$schema _create = "";
$temp = "";
$result = Mysql_db_query ($db, "SELECT * from $table");
$i = 0;
while ($row = Mysql_fetch_row ($result))
{
$schema _insert = "INSERT INTO $table VALUES (";
for ($j = 0; $j <mysql_num_fields ($result); $j + +)
{
if (!isset ($row [$j]))
$schema _insert. = "NULL,";
ElseIf ($row [$j]!= "")
$schema _insert. = "'" Addslashes ($row [$j]). "', ';
Else
$schema _insert. = "',";
}
$schema _insert = Ereg_replace (", $", "", $schema _insert);
$schema _insert. = "); $crlf";
$temp = $temp. $schema _insert;
$i + +;
}
return $temp;
}
?>
Contact qq:31443433