View Single Post
  #2   IP: 153.99.80.189
Old 2016-08-17, 12:37 PM
Uniopolis Uniopolis is offline
初级会员
 
Join Date: 2010-01-28
Posts: 1
Uniopolis 现在声名狼藉
Default

You can open two connections. Use one to read from the source server, the other two insert into the destination server. Use the ON DUPLICATE KEY IGNORE option to prevent errors when you try to overwrite existing rows, so it only inserts the missing rows.

Code:
$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 INTO yourTable (col1, col2, col3, ...) VALUES (:col1, :col2, :col3, ...) ON DUPLICATE KEY IGNORE");
$select_results = $pdo1->query("SELECT * FROM yourTable");
while ($row = $select_results->fetch(PDO::FETCH_ASSOC)) {
    $insert_stmt->execute($row);
}
Reply With Quote