简体   繁体   English

如何在PHP MYSQL中将数据库表和每条记录从一个数据库服务器复制到另一个数据库服务器?>

[英]How to copy database tables and each record from one database server to another database server in PHP MYSQL ?>

Hi I have write a code that can copy database table from one sever to another but the record of each table did not copy how to write a function that can copy tables and each record from one db server to another? 您好我已经编写了一个代码,可以将数据库表从一个服务器复制到另一个服务器,但每个表的记录没有复制如何写一个函数,可以将表和每个记录从一个数据库服务器复制到另一个数据库?

here's my sample code: 这是我的示例代码:

<?php
$dbNewDB = 'newdb';
$dbNewUser = 'newroot';
$dbNewUserPswd = 'newpass';

$dbConnect = mysql_connect('localhost', 'root', 'mypassword') or die('Couldn\'t connect to MySql:'.mysql_error());
$dbNewConnect = mysql_connect('localhost', $dbNewUser, $dbNewUserPswd) or die('Couldn\'t connect to MySql:'.mysql_error());

$sqlStatement = "SHOW TABLES FROM olddb";
$result = mysql_query($sqlStatement,$dbConnect) or die('Unable to get tables: '.mysql_error());
while($row = mysql_fetch_row($result)) 
    {
        //Drop table if exist
        $sqlStatement = "DROP TABLE IF EXISTS " . $dbNewDB . "." . $row[0];
        mysql_query($sqlStatement,$dbNewConnect) or die("Failed to delete: " . mysql_error());
        //Create new table
        $sqlStatement = "CREATE TABLE " . $dbNewDB . "." . $row[0] . " LIKE olddb." . $row[0];
        echo "$sqlStatement [" . __METHOD__ . "]"; 
        mysql_query($sqlStatement,$dbNewConnect)or die("Failed to create: ". mysql_error());
        //Insert data
        $sqlStatement = "INSERT INTO " . $dbNewDB . "." . $row[0] . " SELECT * FROM " . $dbNewDB . "." . $row[0];
        echo "$sqlStatement [" . __METHOD__ . "]"; 
        mysql_query($sqlStatement,$dbNewConnect)or die("Table copy failed: ".mysql_error());
        echo "$row[0] copy done. [" . __METHOD__ . "]"; 
    }

mysql_free_result($result);
mysql_close($dbConnect);
mysql_close($dbNewConnect);

?> 

my code is already functional All i want to fixed to copy the records of each tables. 我的代码已经正常运行所有我想修复以复制每个表的记录。 Any idea?or help? 有什么想法吗?还是帮忙?

Thank you! 谢谢!

You can dump the whole database in SQL format like this: 您可以像下面这样以SQL格式转储整个数据库:

mysqldump --user=root --password=whatever --databases dbtest --opt --quote-names --complete-insert > testbkup.sql

Then you can import it back like this: 然后你可以像这样导入它:

mysql -u root -p whatever dbtest < testbkup.sql

(Note: user = root, password = whatever, dbtest is your database.) (注意:user = root,password = whatever,dbtest是你的数据库。)

Just saying. 只是说。

I found these script working, you can try these: 我发现这些脚本有效,你可以尝试这些:

<?php
$connect2 = mysql_connect("localhost", "root", "");
$database1 = "test1"; // destination database
mysql_select_db($database1, $connect2);
set_time_limit(0);

$database = 'dev_loribonn'; //original database
$connect = mysql_connect("localhost", "root", "");

mysql_select_db($database, $connect);

$tables = mysql_query("SHOW TABLES FROM $database");

while ($line = mysql_fetch_row($tables)) {
    $tab = $line[0];
    mysql_query("DROP TABLE IF EXISTS $database1.$tab");
    mysql_query("CREATE TABLE $database1.$tab LIKE $database.$tab") or die(mysql_error());
    mysql_query("INSERT INTO $database1.$tab SELECT * FROM $database.$tab");
    echo "Table: <b>" . $line[0] . " </b>Done<br>";
}
?>

A side note, if you're just copying the entire table from one database to another, you can do it all in one sql query. 请注意,如果您只是将整个表从一个数据库复制到另一个数据库,则可以在一个SQL查询中完成所有操作。

 CREATE TABLE `backup_db.backup_table` SELECT * FROM `live_db.live_table`; 

I've used something similar for a backup but I dumped the entire db to a backup db 我已经使用类似的东西进行备份,但我将整个数据库转储到备份数据库

Edit: Woops, didn't see the multiple db connections. 编辑:Woops,没有看到多个数据库连接。

Your insert statement looks off. 您的插入语句会关闭。 Trying to insert into $dbNewDB with your values coming from dbNewDB. 尝试使用来自dbNewDB的值插入$ dbNewDB。 You have to turn to the old database. 您必须转向旧数据库。 Below I am building two stings for the insert. 下面我正在为插入物构建两个叮咬。 $string1 = '(col1name, col2name...,)' $string2 = '(val1-1, val1-2, ...), (val2-1, val2-2,...), ...' for "INSERT INTO table $string1 VALUES $string2" $ string1 ='(col1name,col2name ...,)'$ string2 ='(val1-1,val1-2,...),(val2-1,val2-2,...),...' for“INSERT INTO table $ string1 VALUES $ string2”

    //Insert data 

    $sql2 = "SELECT * FROM " . $row[0];
    $r = mysql_query($sql, $bConnect);
    $string1 = '('; $arr = array();
    while ($irow = mysql_fetch_assoc($r)) {$arr[] = $irow;}
    foreach($irow as $k=>$v)
    {
         $string1 .= "$k,";
    }
    $string1 = substr($string1, 0, -1) //lose last comma
    $string1 .= ')';
    $string2 = array_reduce($f, $arr); 
    $string2 = substr($string2, 0, -1) //lose last comma


    $sqlStatement = "INSERT INTO " . $dbNewDB . "." . $row[0] . " $string1 VALUES $string2";
    echo "$sqlStatement [" . __METHOD__ . "]";  
    mysql_query($sqlStatement,$dbNewConnect)or die("Table copy failed: ".mysql_error()); 
    echo "$row[0] copy done. [" . __METHOD__ . "]";  

declare $f elsewhere 在别处宣布$ f

$f = function($b, $x) {$a = ' ('; foreach($x as $v) {$a .= "'$v',";} $a = substr($a, 0, -1); $a .= ')'; return "$b $a,";}

您可以使用sql yog将表传输到不同的主机,而无需在php中编写代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM