0

0

PHP备份数据库为SQL文件

大家讲道理

大家讲道理

发布时间:2016-11-09 14:55:56

|

1284人浏览过

|

来源于php中文网

原创

query("SHOW CREATE TABLE $table");  
         $create = $db->fetch_row($createtable);  
         $tabledump .= $create[1].";\n\n";  
          return $tabledump;  
      }  
      
      
     /****** 备份数据库结构和所有数据 ******/
      /* 
      函数名称:data2sql() 
      函数功能:把表的结构和数据转换成为SQL 
      函数参数:$table: 要进行提取的表名 
      返 回 值:返回提取后的结果,SQL集合 
      函数作者:heiyeluren 
      */
     function data2sql($table)  
      {  
          global $db;  
         $tabledump = "DROP TABLE IF EXISTS $table;\n";  
         $createtable = $db->query("SHOW CREATE TABLE $table");  
         $create = $db->fetch_row($createtable);  
         $tabledump .= $create[1].";\n\n";  
      
         $rows = $db->query("SELECT * FROM $table");  
         $numfields = $db->num_fields($rows);  
         $numrows = $db->num_rows($rows);  
          while ($row = $db->fetch_row($rows))  
          {  
             $comma = "";  
             $tabledump .= "INSERT INTO $table VALUES(";  
              for($i = 0; $i < $numfields; $i++)  
              {  
                 $tabledump .= $comma."'".mysql_escape_string($row[$i])."'";  
                 $comma = ",";  
              }  
             $tabledump .= ");\n";  
          }  
         $tabledump .= "\n";  
      
          return $tabledump;  
      }  
?>  
    

	
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号