【问题标题】:PHP/MySQL: Copy Table and Data from one Database to anotherPHP/MySQL:将表和数据从一个数据库复制到另一个数据库
【发布时间】:2011-11-07 04:22:55
【问题描述】:

另一个问题:从一个数据库中复制整个表而不使用这样的东西的最佳方法是什么:

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

这不起作用,因为我不能同时使用来自两个数据库的数据。所以我决定用 php 来做这个。在那里我必须缓存所有数据,然后在另一个数据库中创建一个表。

但是现在 - 缓存数据的最快方法是什么?我猜每张表的记录几乎每次都少于 1000 条。

感谢您的意见

【问题讨论】:

标签: php mysql


【解决方案1】:

将其导出为 .sql 文件并导入新数据库。

在phpMyAdmin中点击你要导出的数据库,点击export,勾选save as file,然后go

然后将其上传到您要在其中复制数据的新数据库。


在严格的 PHP 意义上,我能想到的唯一方法是使用 SHOW TABLESdescribe {$table} 返回记录的所有字段名称和结构,解析出来,创建你的 @ 987654324@ 语句,然后遍历每个表并创建insert 语句。

恐怕我能为您做的最好的事情是一种原型代码,我认为它会难以置信服务器密集型,这就是为什么我建议你另辟蹊径。

类似:

<?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 script,可能会派上用场以供参考。

希望这可以帮助您入门,但我建议您寻找非 PHP 替代方案。

【讨论】:

  • 嗯,这应该是 php 中的工作,不需要用户输入
【解决方案2】:

【讨论】:

  • 但是我不能使用这样的命令,唯一的可能就是用 PHP 以某种方式做到这一点......
【解决方案3】:
$servername = "localhost";
$username = "root";
$password = "*******";
$dbname = "dbname";
$sqlfile = "/path/backupsqlfile.sql";

$command='mysql -h' .$servername .' -u' .$username .' -p' .$password .' ' .$dbname .' < ' .$sqlfile;
exec($command);

【讨论】:

    猜你喜欢
    • 2011-04-25
    • 1970-01-01
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多