PHP:将MySQL数据库表复制到带有其他列的新数据库表中

时间:2019-04-30 11:39:07

标签: php mysql insert-into

我正在寻找一种将具有多个表的现有数据库复制到具有相同表和列+一些其他列的新数据库中的方法。到目前为止,一切都很好。如果我只是将数据库复制到具有相同数量的表和列的新数据库,则我将这样做:

+---------+---------+---------+
| TABLE 1 |         |         |
+---------+---------+---------+
| Col1    | Col2    | Col3    |
| Value 1 | Value 2 | Value 3 |
| Value 1 | Value 2 | Value 3 |
| Value 1 | Value 2 | Value 3 |
+---------+---------+---------+

复制到:

+---------+---------+---------+
| TABLE 2 |         |         |
+---------+---------+---------+
| Col1    | Col2    | Col3    |
| Value 1 | Value 2 | Value 3 |
| Value 1 | Value 2 | Value 3 |
| Value 1 | Value 2 | Value 3 |
+---------+---------+---------+

代码:

public function loadDB($db1,$db2){
    $this->db->prepare("use ".$db1."");
    $sqlshow = "SHOW TABLES ";
    $statement = $this->db->prepare($sqlshow);
    $statement->execute();
    $tables = $statement->fetchAll(PDO::FETCH_NUM);

    foreach($tables as $table){
        $sql[] = "INSERT INTO ".$db2.".".$table[0]." SELECT * FROM ".$db1.".".$table[0]."; ";
    }
    $sqlState = implode(' ', $sql);
    $insertStatement = $this->db->exec($sqlState);
    return $insertStatement?$insertStatement:false;
}

此代码有效,并且我的数据库已被所有表和表中的值成功复制。现在,我需要一个有效的示例,说明如何将数据库复制到新数据库,其中所有表都有四个附加列,如下所示:

+---------+---------+---------+
| TABLE 1 |         |         |
+---------+---------+---------+
| Col1    | Col2    | Col3    |
| Value 1 | Value 2 | Value 3 |
| Value 1 | Value 2 | Value 3 |
| Value 1 | Value 2 | Value 3 |
+---------+---------+---------+

复制到:

+---------+---------+---------+-----------+-----------+-----------+-----------+
| TABLE 2 |         |         |           |           |           |           |
+---------+---------+---------+-----------+-----------+-----------+-----------+
| Col1    | Col2    | Col3    | Counter   | LoadDay   | User     | UserNew      |
| Value 1 | Value 2 | Value 3 | NEW VALUE | NEW VALUE | NEW VALUE | NEW VALUE |
| Value 1 | Value 2 | Value 3 | NEW VALUE | NEW VALUE | NEW VALUE | NEW VALUE |
| Value 1 | Value 2 | Value 3 | NEW VALUE | NEW VALUE | NEW VALUE | NEW VALUE |
+---------+---------+---------+-----------+-----------+-----------+-----------+

代码(到目前为止,我已经尝试过):

public function loadDB($db1,$db2,$condition){
    $this->db->prepare("use ".$db1."");
    $sqlshow = "SHOW TABLES ";
    $statement = $this->db->prepare($sqlshow);
    $statement->execute();
    $tables = $statement->fetchAll(PDO::FETCH_NUM);

    foreach($tables as $table){
        $sqlshow2 = "SHOW COLUMNS FROM ".$table[0]." ";
        $statement = $this->db->prepare($sqlshow2);
        $statement->execute();
        $columns = $statement->fetchAll(PDO::FETCH_NUM);

        foreach($columns as $column){
            $sql[] = "INSERT INTO ".$db2.".".$table[0]." SELECT ".$column[0]." FROM ".$db1.".".$table[0]."; ";
        }
        $sql[] .= "INSERT INTO ".$db2.".".$table[0]." (`Counter`, `LoadDay`, `User`, `UserNew`) VALUES ('1', '".date("Y-m-d H:i:s")."', '".$condition."', '".$condition."')";
    }
    $sqlState = implode(' ', $sql);
    var_dump($sqlState);
    $insertStatement = $this->db->exec($sqlState);
    return $insertStatement?$insertStatement:false;
}

正在创建数据库(在我这里发布的代码中看不到)。我只不会将任何值复制到新数据库中的新表中。我在这里做什么错了?

1 个答案:

答案 0 :(得分:1)

您的最终SQL查询出错。我建议将您的代码更改为这样:

foreach($tables as $table){
    $sqlshow2 = "SHOW COLUMNS FROM ".$table[0]." ";
    $statement = $this->db->prepare($sqlshow2);
    $statement->execute();
    $columns = $statement->fetchAll(PDO::FETCH_NUM);

    $sql[] = "INSERT INTO ".$db2.".".$table[0]." SELECT * , '1', '".date("Y-m-d H:i:s")."', '".$condition."', '".$condition."'" . " FROM ".$db1.".".$table[0]."; ";

}