2

我必须创建数据库表的克隆。我使用 wordpress 数据库,我想创建一个 WP-options 表的克隆。如何在数据库中创建特定表的克隆。我找到了一些代码

set_time_limit(0);

$username = 'XXXXXX';
$password = 'YYYYYY';
$hostname = 'ZZZZZZ';
$database = 'AAAAAA';

try {
    $pdo = new PDO("mysql:host={$hostname};dbname={$database}", $username, $password);
}
catch(PDOException $e) {
    die("Could not connect to the database\n");
}

echo '<pre>';
$stmt1 = $pdo->query('SHOW TABLES', PDO::FETCH_NUM);
foreach($stmt1->fetchAll() as $row) {
    $stmt2 = $pdo->query("SHOW CREATE TABLE `$row[0]`", PDO::FETCH_ASSOC);
    $table = $stmt2->fetch();
    echo "{$table['Create Table']};\n\n";
}
echo '</pre>';

但我不明白如何在我给出的路径的文件夹中获取数据库。

4

2 回答 2

0

我在谷歌搜索几个小时之前和之后遇到了同样的问题,我最终得到了下面的代码。

希望这会有所帮助。

好极客很抱歉使用mysql_**它已弃用。

<?php
$DBIUser = 'someuser';
$DBIPass = 'thepassword';

$NewUser = 'someloser';
$NewPass = 'thepassword';

$oldServer = 'my crappy old mysql server domain';
$newServer = 'localhost';

if ($argv[0] > " ")
{
    $dbname = $argv[1];
    echo "Starting copy of the $argv[1] database.\n";
    $dbpre = mysql_connect($oldServer, $DBIUser, $DBIPass);
    mysql_select_db($dbname, $dbpre);
    $sql = "SHOW TABLES FROM $dbname";
    echo $sql."\n";
    $result = mysql_query($sql);

    if (!$result)
    {
        echo "DB Error, could not list tables\n";
        echo 'MySQL Error: ' . mysql_error();
        exit;
    }

    $dbtbl = mysql_connect($oldServer, $DBIUser, $DBIPass);
    mysql_select_db($dbname, $dbpre);
    $dbnew = mysql_connect($newServer, $NewUser, $NewPass);
    mysql_select_db("mysql", $dbnew);

    $res2 = mysql_query("CREATE DATABASE IF NOT EXISTS ".$dbname,$dbnew);
    if (!$res2)
    {
            echo "DB Error, could not create database\n";
            echo 'MySQL Error: ' . mysql_error();
            exit;
    }
    mysql_select_db($dbname, $dbnew);


    if($result === FALSE)
    {
        die(mysql_error());
    }

    $f = fopen($dbname.'.log', 'w');
    fwrite($f, "Copy all tables in database $dbname on server $oldServer to new database on server $newServer.\n\n");
    while ($row = mysql_fetch_row($result))
    {
        echo "Table: {$row[0]}\n";
        fwrite($f, "Table ".$row[0]."\n");
        $tableinfo = mysql_fetch_array(mysql_query("SHOW CREATE TABLE $row[0]  ",$dbtbl));
        $createsyntax = "CREATE TABLE IF NOT EXISTS ";
        $createsyntax .= substr($tableinfo[1], 13);

        mysql_query(" $createsyntax ",$dbnew);

        $res = mysql_query("SELECT * FROM $row[0]  ",$dbpre); // select all rows
        $oldcnt = mysql_num_rows($res);
        echo "Count: ".$oldcnt." - ";

        $errors = 0;
        while ($roz = mysql_fetch_array($res, MYSQL_ASSOC) )
        {
          $query =  "INSERT INTO $dbname.$row[0] (".implode(", ",array_keys($roz)).") VALUES (";
          $cnt = 0;
          foreach (array_values($roz) as $value)
          {
            if ($cnt == 0)
            {
              $cnt++;
            } else
            {
              $query .= ",";
            }
            $query .= "'";
            $query .= mysql_real_escape_string($value);
            $query .= "'";

          }
          $query .= ")";

          $look = mysql_query($query,$dbnew);
          if ($look === false)
          {
            // write insert to log on error
            $errors = $errors + 1;
            fwrite($f, mysql_error()." - ".$query."\n");
          }

        }
        $sql = "select count(*) as cnt from $dbname.$row[0] ";
        $res = mysql_query($sql, $dbnew);
        $roz = mysql_fetch_array($res);
        echo $roz['cnt']." - Errors: ".$errors."\n";
        fwrite($f, "Old Record Count: ".$oldcnt." - New Record Count: ".$roz['cnt']." - Errors: ".$errors."\n");
        fwrite($f,"End table copy for table $row[0].\n\n");

    }
    fclose($f);
}
else
{
    var_dump($argv);
}
?>
于 2013-02-12T06:12:16.687 回答
0

我不确定它是关于一个表还是整个数据库,因为您要求一个表,但显示了一个试图复制完整数据库的代码。

这是我使用 PHP 和 MySQLi 复制完整数据库的脚本:

// Database variables

$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = '1234';

$DB_SRC = 'existing_db';
$DB_DST = 'newly_created_db';



// MYSQL Connect

$mysqli = new mysqli( $DB_HOST, $DB_USER, $DB_PASS ) or die( $mysqli->error );



// Create destination database

$mysqli->query( "CREATE DATABASE $DB_DST" ) or die( $mysqli->error );



// Iterate through tables of source database

$tables = $mysqli->query( "SHOW TABLES FROM $DB_SRC" ) or die( $mysqli->error );

while( $table = $tables->fetch_array() ): $TABLE = $table[0];


    // Copy table and contents in destination database

    $mysqli->query( "CREATE TABLE $DB_DST.$TABLE LIKE $DB_SRC.$TABLE" ) or die( $mysqli->error );
    $mysqli->query( "INSERT INTO $DB_DST.$TABLE SELECT * FROM $DB_SRC.$TABLE" ) or die( $mysqli->error );


endwhile;

如果您只想复制一个表,您可以将其修改为如下所示(目标表必须存在于此代码中):

// Database variables

$DB_HOST = 'localhost';
$DB_USER = 'root';
$DB_PASS = '1234';

$DB_SRC = 'existing_db';
$DB_DST = 'newly_created_db';

$DB_TABLE = 'wp_options';


// MYSQL Connect

$mysqli = new mysqli( $DB_HOST, $DB_USER, $DB_PASS ) or die( $mysqli->error );


// Copy table and contents in destination database

$mysqli->query( "CREATE TABLE $DB_DST.$DB_TABLE LIKE $DB_SRC.$DB_TABLE" ) or die( $mysqli->error );
$mysqli->query( "INSERT INTO $DB_DST.$DB_TABLE SELECT * FROM $DB_SRC.$DB_TABLE" ) or die( $mysqli->error );
于 2019-09-13T06:26:28.197 回答