zoukankan      html  css  js  c++  java
  • PHP导出数据库方法

    (1)定义一个db_dump函数如下:

    <?PHP
    function db_dump($host,$user,$pwd,$db) {
        $mysqlconlink = mysql_connect($host,$user,$pwd , true);
        if (!$mysqlconlink)
            echo sprintf('No MySQL connection: %s',mysql_error())."<br/>";
        mysql_set_charset( 'utf8', $mysqlconlink );
        $mysqldblink = mysql_select_db($db,$mysqlconlink);
        if (!$mysqldblink)
            echo sprintf('No MySQL connection to database: %s',mysql_error())."<br/>";
        $tabelstobackup=array();
        $result=mysql_query("SHOW TABLES FROM `$db`");
        if (!$result)
            echo sprintf('Database error %1$s for query %2$s', mysql_error(), "SHOW TABLE STATUS FROM `$db`;")."<br/>";
        while ($data = mysql_fetch_row($result)) {
                $tabelstobackup[]=$data[0];
        }
        if (count($tabelstobackup)>0) {
            $result=mysql_query("SHOW TABLE STATUS FROM `$db`");
            if (!$result)
                echo sprintf('Database error %1$s for query %2$s', mysql_error(), "SHOW TABLE STATUS FROM `$db`;")."<br/>";
            while ($data = mysql_fetch_assoc($result)) {
                $status[$data['Name']]=$data;
            }
            if ($file = fopen("$db.sql", 'wb')) {
                fwrite($file, "-- ---------------------------------------------------------
    ");
                fwrite($file, "-- Database Name: $db
    ");
                fwrite($file, "-- ---------------------------------------------------------
    
    ");
                fwrite($file, "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    ");
                fwrite($file, "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    ");
                fwrite($file, "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    ");
                fwrite($file, "/*!40101 SET NAMES '".mysql_client_encoding()."' */;
    ");
                fwrite($file, "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
    ");
                fwrite($file, "/*!40103 SET TIME_ZONE='".mysql_result(mysql_query("SELECT @@time_zone"),0)."' */;
    ");
                fwrite($file, "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
    ");
                fwrite($file, "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
    ");
                fwrite($file, "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
    ");
                fwrite($file, "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
    
    ");
                foreach($tabelstobackup as $table) {
                    echo sprintf('Dump database table "%s"',$table)."<br/>";
                    need_free_memory(($status[$table]['Data_length']+$status[$table]['Index_length'])*3);
                    _db_dump_table($table,$status[$table],$file);
                }
                fwrite($file, "
    ");
                fwrite($file, "/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
    ");
                fwrite($file, "/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
    ");
                fwrite($file, "/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
    ");
                fwrite($file, "/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
    ");
                fwrite($file, "/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    ");
                fwrite($file, "/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    ");
                fwrite($file, "/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
    ");
                fwrite($file, "/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
    ");
                fclose($file);
                echo 'Database dump done!'."<br/>";
            } else {
                echo 'Can not create database dump!'."<br/>";
            }
        } else {
            echo 'No tables to dump'."<br/>";
        }
    }
    function _db_dump_table($table,$status,$file) {
        fwrite($file, "
    ");
        fwrite($file, "--
    ");
        fwrite($file, "-- Table structure for table $table
    ");
        fwrite($file, "--
    
    ");
        fwrite($file, "DROP TABLE IF EXISTS `" . $table .  "`;
    ");
        fwrite($file, "/*!40101 SET @saved_cs_client     = @@character_set_client */;
    ");
        fwrite($file, "/*!40101 SET character_set_client = '".mysql_client_encoding()."' */;
    ");
        $result=mysql_query("SHOW CREATE TABLE `".$table."`");
        if (!$result) {
            echo sprintf('Database error %1$s for query %2$s', mysql_error(), "SHOW CREATE TABLE `".$table."`")."<br/>";
            return false;
        }
        $tablestruc=mysql_fetch_assoc($result);
        fwrite($file, $tablestruc['Create Table'].";
    ");
        fwrite($file, "/*!40101 SET character_set_client = @saved_cs_client */;
    ");
        $result=mysql_query("SELECT * FROM `".$table."`");
        if (!$result) {
            echo sprintf('Database error %1$s for query %2$s', mysql_error(), "SELECT * FROM `".$table."`")."<br/>";
            return false;
        }
        fwrite($file, "--
    ");
        fwrite($file, "-- Dumping data for table $table
    ");
        fwrite($file, "--
    
    ");
        if ($status['Engine']=='MyISAM')
            fwrite($file, "/*!40000 ALTER TABLE `".$table."` DISABLE KEYS */;
    ");
        while ($data = mysql_fetch_assoc($result)) {
            $keys = array();
            $values = array();
            foreach($data as $key => $value) {
                if($value === NULL)
                    $value = "NULL";
                elseif($value === "" or $value === false)
                    $value = "''";
                elseif(!is_numeric($value))
                    $value = "'".mysql_real_escape_string($value)."'";
                $values[] = $value;
            }
            fwrite($file, "INSERT INTO `".$table."` VALUES ( ".implode(", ",$values)." );
    ");
        }
        if ($status['Engine']=='MyISAM')
            fwrite($file, "/*!40000 ALTER TABLE ".$table." ENABLE KEYS */;
    ");
    }
    function need_free_memory($memneed) {
        if (!function_exists('memory_get_usage'))
            return;
        $needmemory=@memory_get_usage(true)+inbytes($memneed);
        if ($needmemory>inbytes(ini_get('memory_limit'))) {
            $newmemory=round($needmemory/1024/1024)+1 .'M';
            if ($needmemory>=1073741824)
                $newmemory=round($needmemory/1024/1024/1024) .'G';
            if ($oldmem=@ini_set('memory_limit', $newmemory))
                echo sprintf(__('Memory increased from %1$s to %2$s','backwpup'),$oldmem,@ini_get('memory_limit'))."<br/>";
            else
                echo sprintf(__('Can not increase memory limit is %1$s','backwpup'),@ini_get('memory_limit'))."<br/>";
        }
    }
    function inbytes($value) {
        $multi=strtoupper(substr(trim($value),-1));
        $bytes=abs(intval(trim($value)));
        if ($multi=='G')
            $bytes=$bytes*1024*1024*1024;
        if ($multi=='M')
            $bytes=$bytes*1024*1024;
        if ($multi=='K')
            $bytes=$bytes*1024;
        return $bytes;
    }
    ?>

    (2)使用方法:

    db_dump('数据库服务器', '数据库用户名', '数据库密码', '数据库名');
  • 相关阅读:
    求和:1-2+3-4+……+m
    倒序输出字符串
    Lamdba表达式
    扩展方法 extension method
    命名参数 named parameter
    动态查找 dynamic
    Git命令与介绍
    Excel函数和公式(四)——Excel基础(4)
    Excel函数和公式(三)——Excel基础(3)
    Excel函数和公式(二)——Excel基础(2)
  • 原文地址:https://www.cnblogs.com/flying_bat/p/3324793.html
Copyright © 2011-2022 走看看