Last active
June 1, 2018 20:07
-
-
Save mdeora/919f4bb00512daef6504cbc973bbef4a to your computer and use it in GitHub Desktop.
Copy all tables from one database to another database using php code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$dblink1=mysqli_connect('[server ip1]', '[username]', '[password]'); // connect server 1 | |
mysqli_select_db($dblink1,'[db name]'); // select database 1 | |
$dblink2=mysqli_connect('[server ip2]', '[username]', '[password]'); // connect server 2 | |
mysqli_select_db($dblink2,'[db name]'); // select database 2 | |
$tables = mysqli_fetch_array(mysqli_query($dblink1,"SHOW TABLES ")); | |
//$table='tabletest'; | |
foreach($tables as $table){ | |
$tableinfo = mysqli_fetch_array(mysqli_query($dblink1,"SHOW CREATE TABLE $table ")); // get structure from table on server 1 | |
mysqli_query($dblink2," $tableinfo[1] "); // use found structure to make table on server 2 | |
$result = mysqli_query($dblink1,"SELECT * FROM $table "); // select all content | |
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) { | |
mysqli_query($dblink2,"INSERT INTO $table (".implode(", ",array_keys($row)).") VALUES ('".implode("', '",array_values($row))."')"); // insert one row into new table | |
} | |
} | |
mysqli_close($dblink1); | |
mysqli_close($dblink2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment