dongxiatuo9827 2011-08-30 21:32
浏览 417
已采纳

PHP / MySQL:将表和数据从一个数据库复制到另一个数据库

Anoter question: What is the best way to copy a whole table from one Database without using something like this:

CREATE TABLE DB2.USER SELECT * FROM DB1.USER;

This does not work because I can't use data from two Databases at the same time. So I decided to make this with php. There I had to cache all Data and then I'd create a table in the other DB.

But now - what would be the fastest way to cache the data? I guess there are closely everytime less than 1000 records per table.

Thanks for your input

  • 写回答

3条回答 默认 最新

  • dtvpl739577 2011-08-30 21:34
    关注

    Export it to a .sql file and import to the new database.

    In phpMyAdmin click the database you want to export, click <kbd>export</kbd>, check <kbd>save as file</kbd>, then <kbd>go</kbd>.

    Then upload it to the new database that you want to duplicate the data in.


    In a strict PHP sense, the only way I could think to do it would to be use of SHOW TABLES and describe {$table} to return the all the field names and structures of the records, parse that out, create your create table statements, then loop through each table and create insert statements.

    I'm afraid the best I can do for you is a sort of prototype code that I would imagine would be incredibly server intensive, which is why I recommend you go an alternate route.

    Something like:

    <?php
    
        // connect to first DB
    
        $tables = mysql_query("SHOW TABLES") or die(mysql_error());
        while ($row = mysql_fetch_assoc($tables)) {
            foreach($row as $value) {
                $aTables[] = $value;
            }
        }
        $i = 0;
        foreach ($aTables as $table) {
            $desc = mysql_query("describe " . $table);
            while ($row = mysql_fetch_assoc($desc)) {
                $aFields[$i][] = array($row["Field"],$row["Type"],$row["Null"],$row["Key"],$row["Default"],$row["Extra"]);
            }
            $i++;
        }
    
        // connect to second DB
    
        for ($i = 0; $i < count($aTables); $i++) {
    
            // Loop through tables, fields, and rows for create table/insert statements
    
            $query = 'CREATE TABLE IF NOT EXISTS {$aTables[$i]} (
                    //loop through fields {
    
                        {$aFields[$i][$j][0]} {$aFields[$i][$j][1]} {$aFields[$i][$j][2]} {$aFields[$i][$j][3]} {$aFields[$i][$j][4]} {$aFields[$i][$j][5]},
                        {$aFields[$i][$j][0]} {$aFields[$i][$j][1]} {$aFields[$i][$j][2]} {$aFields[$i][$j][3]} {$aFields[$i][$j][4]} {$aFields[$i][$j][5]},
                        {$aFields[$i][$j][0]} {$aFields[$i][$j][1]} {$aFields[$i][$j][2]} {$aFields[$i][$j][3]} {$aFields[$i][$j][4]} {$aFields[$i][$j][5]},
                        {$aFields[$i][$j][0]} {$aFields[$i][$j][1]} {$aFields[$i][$j][2]} {$aFields[$i][$j][3]} {$aFields[$i][$j][4]} {$aFields[$i][$j][5]},
                        etc...
                    }
                )';
    
                //loop through data
    
                $query .= 'INSERT INTO {$aTables[$i]} VALUES';
                $result = mysql_query("SELECT * FROM " . $aTables[$i]);
                while ($row = mysql_fetch_assoc($result)) {
                        $query .= '(';
                    foreach ($aFields[$i][0] as $field) {
    
                        $query .= '"{$row[$field]}",';
                    }
                    $query .= '),';
                }
            mysql_query($query);
        }
    
    ?> 
    

    This is based off of this script which may come in handy for reference.

    Hopefully that's something to get you started, but I would suggest you look for a non PHP alternative.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)