PHP Classes

File: Fogg/Db/Sync/example.php

Recommend this page to a friend!
  Classes of Jacob Fogg   PHP Database Sync   Fogg/Db/Sync/example.php   Download  
File: Fogg/Db/Sync/example.php
Role: Example script
Content type: text/plain
Description: An example file
Class: PHP Database Sync
Synchronize tables of different databases with PDO
Author: By
Last change:
Date: 9 years ago
Size: 967 bytes
 

Contents

Class file image Download
<?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');