PHP Classes

PHP Database Sync: Synchronize tables of different databases with PDO

Recommend this page to a friend!
  Info   View files Example   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 461 This week: 1All time: 6,075 This week: 560Up
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  
Name: Jacob Fogg <contact>
Classes: 7 packages by
Country: United States United States
Age: 44
All time rank: 1049149 in United States United States
Week rank: 416 Up46 in United States United States Up
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  
File Role Description
Files folder imageFogg (1 directory)

  Files folder image Files  /  Fogg  
File Role Description
Files folder imageDb (1 directory)

  Files folder image Files  /  Fogg  /  Db  
File Role Description
Files folder imageSync (2 files)

  Files folder image Files  /  Fogg  /  Db  /  Sync  
File Role Description
  Accessible without login Plain text file example.php Example An example file
  Plain text file Sync.php Class The main class file

 Version Control Unique User Downloads Download Rankings  
 0%
Total:461
This week:1
All time:6,075
This week:560Up