• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

如何用PHP的页面备份、恢复Mysql数据库_php

php 搞代码 7年前 (2018-06-19) 147次浏览 已收录 0个评论

<?        
  //备份数据  
    $i   =   0;  
    $crlf=”/r/n”;  
    $dbname=”xgtqr”;  
    global     $dbconn;  

http://www.gaodaima.com/46604.html如何用php的页面备份、恢复mysql数据库_php

    $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++;  
          }  
   
  //定义抽取表结构与数据  
   
   
  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<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;  
  }  
  ?>  
联系QQ:31443433

欢迎大家阅读《如何用PHP的页面备份、恢复Mysql数据库_php,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:如何用PHP的页面备份、恢复Mysql数据库_php
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址