Home > Backend Development > PHP Tutorial > Specific implementation of php exporting the entire mysql database to generate sql files_PHP tutorial

Specific implementation of php exporting the entire mysql database to generate sql files_PHP tutorial

WBOY
Release: 2016-07-13 10:41:41
Original
709 people have browsed it

Found on the Internet, there have been changes.

File name: db_backup.php

The source code is as follows:

Copy code The code is as follows:

ini_set("max_execution_time", "180");//Avoid excessive data volume and incomplete export.

/*

Program function: mysql database backup function
Author: Tang Xiaogang
Description:
This program is mainly extracted from mysqladmin and made certain adjustments , I hope it will be helpful to everyone when backing up data when programming in PHP.
If you do not want to back up the structure: please delete this sentence: echo get_table_structure($dbname, $table, $crlf).";$crlf$crlf";
If you do not want to back up content: please screen this sentence: echo get_table_content($dbname, $table, $crlf);

Modifier: He Jinsheng
Modification time: 2009/11/7
Modified content: Added function get_table_structure, commented out function get_table_def, in order to obtain richer details when creating a table (such as: ENGINE=InnoDB AUTO_INCREMENT=80 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Product information change information' )
*/

$host="";//Database address

$dbname="";//Configure the database name here

$username=" ";//Username

$passw="";//Configure password here

$filename=date("Y-m-d_H-i-s")."-".$dbname. ".sql";
header("Content-disposition:filename=".$filename);//The saved file name
header("Content-type:application/octetstream"); ​​
header ("Pragma:no-cache");
header("Expires:0");

//Backup data
$i = 0;
$crlf="rn";
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++;
}

/*Newly added get 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;
}

/*
//Others originally obtained the database structure, but it was incomplete
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 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 content
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{
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;
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/676878.htmlTechArticle was found online and has been changed. File name: db_backup.php The source code is as follows: Copy the code as follows: ?php ini_set("max_execution_time", "180");//Avoid excessive data volume and incomplete export...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template