search
Homephp教程php手册如何用PHP的页面备份、恢复Mysql数据库中的数据表

mysql|备份|恢复|数据|数据库|页面

       
  //备份数据  
    $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             {    
                  $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++;  
          }  
   
  //定义抽取表结构与数据  
   
   
  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   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));  
  }  
   
  //保存数据表的值  
  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;  
  }  
  ?>  
联系QQ:31443433



Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools