How to use PHP pages to back up and restore the data tables in the MySQL database

Source: Internet
Author: User
Tags empty mysql mysql database
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.