PHP to export the MySQL database whole library to generate SQL file detailed code

Source: Internet
Author: User

  Below is PHP's detailed code to export the MySQL database to generate SQL files, which hopefully will help you back up your data while you are programming with PHP

From the Internet search, there are changes.     filename:db_backup.php    Source code is as follows:    code as follows: <?php  ini_set ("Max_execution_time", " 180 ")//Avoid large amount of data, the export of incomplete situation appears.    /*    program function: MySQL database backup function   Author: Tang Xiaogang  :  This procedure is mainly extracted from the mysqladmin, and make certain adjustments, I hope we can help you back up your data when you're programming with PHP .  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   Modify time:2009/11/7  Modify content: New function get_table_structure, comment out function get_table_def, the purpose is to obtain richer table details (such as: Engine=innodb auto_ increment=80 DEFAULT Charset=utf8 collate=utf8_unicode_ci comment= ' Commodity Information Change information ')   */    $host = "";//Database Address & nbsp   $dbname = ""//here Config database name     $username = "";/user name     $PASSW = "";//This configuration 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 ");   //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 ++; }   /* New 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"); &nbSp $row =mysql_fetch_array ($result);  $schema _create. = $crlf. -". $row [0]. $crlf;  $schema _create. = $row [1]. $crlf;  return $schema _create; }   /* / Original others get database structure, but incomplete   function get_table_def ($db, $table, $CRLF)   {  global $drop;    $schema _ Create = "";  if (!empty ($drop))   $schema _create. = "Drop TABLE if EXISTS ' $table '; $crlf";    $schema _c reate. = "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 [D Efault] ';  if ($row ["null"]!= "YES")   $schema _create. = "Not Null";  if ($row ["Extra"]!= "")   $schem A_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_arra Y ($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 = ""; &n Bsp $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.