Skip to content

Instantly share code, notes, and snippets.

@PauliusKrutkis
Created August 1, 2018 06:42
Show Gist options
  • Select an option

  • Save PauliusKrutkis/f016fcc75e7db3fd33c9a6a7ec3106b6 to your computer and use it in GitHub Desktop.

Select an option

Save PauliusKrutkis/f016fcc75e7db3fd33c9a6a7ec3106b6 to your computer and use it in GitHub Desktop.
Copy paste tables from one database to another with php
<?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