Created
August 1, 2018 06:42
-
-
Save PauliusKrutkis/f016fcc75e7db3fd33c9a6a7ec3106b6 to your computer and use it in GitHub Desktop.
Copy paste tables from one database to another with php
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 | |
| ini_set('display_errors', 1); | |
| ini_set('display_startup_errors', 1); | |
| error_reporting(E_ALL); | |
| $query = ''; | |
| $source = 'database_one'; | |
| $destination = 'database_two'; | |
| $tables = [ | |
| 'table_one', | |
| 'table_two', | |
| // more tables | |
| ]; | |
| $con = new mysqli("host", "user", "pass", "table"); | |
| if ($con->connect_error) { | |
| die("Connection failed: " . $con->connect_error); | |
| } | |
| $query = "SET FOREIGN_KEY_CHECKS=0;"; | |
| foreach ($tables as $table) { | |
| $query .= " | |
| DROP TABLE IF EXISTS " . $destination . "." . $table . "; | |
| CREATE TABLE " . $destination . "." . $table . " LIKE " . $source . "." . $table . "; | |
| INSERT " . $destination . "." . $table . " SELECT * FROM " . $source . "." . $table . ";"; | |
| } | |
| $query .= "SET FOREIGN_KEY_CHECKS=1;"; | |
| $con->multi_query($query); | |
| if ($con->error) { | |
| echo $con->error; | |
| } | |
| $con->close(); | |
| echo 'done'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment