PHP Classes

PHP Database Sync: Synchronize tables of different databases with PDO

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 465 All time: 6,050 This week: 36Up
Version License PHP version Categories
db-sync 1.0.0GNU General Publi...5.4PHP 5, Databases, Systems administration
Description 

Author

This class can Synchronize tables of different databases with PDO.

It can connect to two different databases given to PDO objects queries a given table of the source database to insert its records on the destination database table of the same name.

The class can truncate the destination table to make a complete copy of the origin table, or delete rows first before inserting the updated rows based on its primary key.

Innovation Award
PHP Programming Innovation award nominee
November 2014
Number 5


Prize: One downloadable copy of Komodo IDE
Database synchronization is a process sometimes used by applications replicate data in databases.

When you are using databases of the same type, database synchronization is often a matter querying the origin database and inserting database records in the destination database.

However, if you need to use databases of different types, some field data type conversion may be necessary.

This class provides a solution to synchronize tables between different databases. It uses PDO to access each database, so it can avoid the need to convert data types.

Manuel Lemos
Picture of Jacob Fogg
  Performance   Level  
Innovation award
Innovation award
Nominee: 3x

Winner: 1x

 

Recommendations

Recommendation for a PHP class to sync database
sync database beetween local machine and server

Example

<?php
/**
 * This example file shows the basic uses of the DB Sync class.
 */

//use and include the Sync class
use Fogg\Db\Sync\Sync;
require
'Sync.php';

//Create two PDO Database connections. In theory, the database type is irrelevant.
$db = new PDO("mysql:host=localhost;dbname=TestDb", 'root', '****');
$db2 = new PDO("mysql:host=localhost;dbname=NewDb", 'root', '*******');

//Instantiate the Sync class
$sync = new Sync($db, $db2);

//Copy all rows from the test table. This will truncate the receiving table prior to insert
$sync->syncTable('test');

//Copy all rows from the test tabe. This will delete rows based on the field id prior to insert
$sync->syncTable('test', '*', '', false, 'id');

//Copy all rows from the test table with an id > 3, truncating the table prior to insert
$sync->syncTable('test', '*', 'id > 3');

//Copy the name and value fields for all rows from the test table
$sync->syncTable('test', 'name, value');


  Files folder image Files (2)  
File Role Description
Files folder imageFogg (1 directory)

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:465
This week:0
All time:6,050
This week:36Up