drmeu26880 2019-04-30 11:39
浏览 64
已采纳

PHP:将MySQL数据库表复制到具有附加列的新数据库表中

I am looking for a way to copy an existing database with multiple tables into a new database with the same tables and columns + some additional columns. So far so good. If I just copy the database to a new database with the same amount of tables and columns I am doing it like this:

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

copy to:

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

Code:

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;
}

This code works and my database is copied successfully with all my tables and values inside my tables. What I need now is a working example of how am I able to copy the database to a new database where all tables have four additional columns like this:

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

copying to:

+---------+---------+---------+-----------+-----------+-----------+-----------+
| 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 |
+---------+---------+---------+-----------+-----------+-----------+-----------+

Code (what I´ve tried so far):

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;
}

The creation of the databases are working (not visible in my posted code here). I only get no values copied to my new tables inside my new database. What am I doing wrong here?

  • 写回答

1条回答 默认 最新

  • douzuqin3467 2019-04-30 11:53
    关注

    Your final SQL query comes out wrong. I suggest changing your code to somthing like this:

    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]."; ";
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘