php - How to copy table from one table on remote server to table on a different remote db mysql5.6 with PDO -


i have matching databases on 2 different servers (mysql replication not option). need see if record missing 1 table2 , if truncate table2 , copy table1 table2. each table on different ip/server.

code:

            $pdoyd = new pdo(                     'mysql:host=' . db_host_yoda . ';dbname=' . db_database_dns,                      db_user_dns,                      db_password             );             $pdoyd->setattribute(pdo::attr_errmode, pdo::errmode_exception);             $pdoyd->setattribute(pdo::attr_emulate_prepares, false);             $truntbl = array('cryptokeys', 'domains', 'records');             foreach($truntbl $tbl){                     $sql = 'truncate '.$tbl;                     $statementyd = $pdoyd->prepare($sql);                     $useryd = $statementyd->execute();                     var_dump($statementyd);                     echo '<br>';             } 

what efficient way copy missing records 1 table on 1 server table on different server?

you can open 2 connections. use 1 read source server, other 2 insert destination server. use on duplicate key ignore option prevent errors when try overwrite existing rows, inserts missing rows.

$pdo1 = new pdo('mysql:host=server1;dbname=xxx', $username1, $password1); $pdo2 = new pdo('mysql:host=servrer2; dbname=xxx', $username2, $password2);  $insert_stmt = $pdo2->prepare("insert yourtable (col1, col2, col3, ...) values (:col1, :col2, :col3, ...) on duplicate key ignore"); $select_results = $pdo1->query("select * yourtable"); while ($row = $select_results->fetch(pdo::fetch_assoc)) {     $insert_stmt->execute($row); } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -