开发者

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

开发者 https://www.devze.com 2023-01-26 09:20 出处:网络
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

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 " 开发者_JS百科. $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:

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.)

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


Your insert statement looks off. Trying to insert into $dbNewDB with your values coming from 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"

    //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 = function($b, $x) {$a = ' ('; foreach($x as $v) {$a .= "'$v',";} $a = substr($a, 0, -1); $a .= ')'; return "$b $a,";}


You can use sql yog to transfer the tables to different host without writing code in php .

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号