PHP to the MySQL database to export the entire library to generate SQL files of the specific implementation of _php skills

Source: Internet
Author: User
Tags php programming
From the Internet search, there are changes.

FileName: db_backup.php

The source code is as follows:
Copy Code code as follows:

<?php
Ini_set ("Max_execution_time", "180");//Avoid large amount of data and export incomplete.

/*

program function: MySQL database backup function
Author: Tang Xiaogang
Description
This program is mainly extracted from the mysqladmin, and make a certain adjustment, I hope for everyone in the use of PHP programming backup data has some help.
If you don't want to back up your structure: Please screen this: Echo get_table_structure ($dbname, $table, $crlf). "; $crlf $crlf ";
If you don't want to back up your content: Please screen this: Echo get_table_content ($dbname, $table, $CRLF);

Modified By: He Jinsheng
Modification Date: 2009/11/7
Modify content: New function get_table_structure, comment out the function get_table_def, the purpose is to get richer details of the build table (such as: Engine=innodb auto_increment=80 DEFAULT Charset=utf8 collate=utf8_unicode_ci comment= ' Commodity Information Change information ')
*/

$host = "";//Database address

$dbname = ""//here Config database name

$username = "";//user name

$PASSW = "";//Here Configure password

$filename =date ("Y-m-d_h-i-s"). " -". $dbname.". SQL ";
Header ("Content-disposition:filename=". $filename);//Saved file name
Header ("Content-type:application/octetstream");
Header ("Pragma:no-cache");
Header ("expires:0");

Backing up data
$i = 0;
$crlf = "\ r \ n";
Global $dbconn;
$dbconn = mysql_connect ($host, $username, $PASSW]);//Database host, username, password
$db = mysql_select_db ($dbname, $dbconn);
mysql_query ("SET NAMES ' UTF8 '");
$tables =mysql_list_tables ($dbname, $dbconn);
$num _tables = @mysql_numrows ($tables);
Print "--filename=". $filename;
while ($i < $num _tables)
{
$table =mysql_tablename ($tables, $i);
Print $crlf;
Echo get_table_structure ($dbname, $table, $crlf). "; $crlf $crlf ";
Echo get_table_def ($dbname, $table, $crlf). "; $crlf $crlf ";
Echo get_table_content ($dbname, $table, $CRLF);
$i + +;
}

/* New access to detailed table structure * *
function Get_table_structure ($db, $table, $crlf)
{
Global $drop;

$schema _create = "";
if (!empty ($drop)) {$schema _create. = "Drop TABLE IF EXISTS ' $table '; $crlf";}
$result =mysql_db_query ($db, "show CREATE TABLE $table");
$row =mysql_fetch_array ($result);
$schema _create. = $crlf. " -". $row [0]. $crlf;
$schema _create. = $row [1]. $crlf;
return $schema _create;
}

/*
Original others get the database structure, but not complete
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 full 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]";
if ($row ["Comment"]!= "")
$schema _create. = "Comment ' $row [Comment] '";
$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));
}
*/

Get table Contents
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;
}
?>
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.