![]() |
|
|||||||
![]() |
|
|
Thread Tools | Display Modes |
|
#1
IP: 153.99.80.189
|
|||
|
|||
|
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 is not an option). I need to see if a record is missing from one table2 and if so truncate table2 and then copy from table1 to table2. each table is on a different IP/server. |
|
#2
IP: 153.99.80.189
|
|||
|
|||
|
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);
}
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Zen Cart 常用SQL命令 | Renton | ZenCart | 0 | 2015-09-29 04:45 AM |
| Simple Steps to Change Your Table Prefix in WordPress | Abby | WordPress | 0 | 2014-04-14 03:40 PM |
| How to Transfer Files/Backup from Server to Server | topvip | 服务器环境搭建 | 0 | 2010-10-20 12:02 AM |
| SQL Server导出导入数据方法 | topvip | 服务器环境搭建 | 0 | 2009-05-06 11:04 AM |