PHP code example of dimtrovich / db-dumper

1. Go to this page and download the library: Download dimtrovich/db-dumper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dimtrovich / db-dumper example snippets


use PDO;

$pdo = new PDO('mysql:host=localhost;port=3307;dbname=database', 'username', 'password');

use Dimtrovich\DbDumper\Exporter;
use Exception;

try {
    $exporter = new Exporter($pdo, 'database');
    
    $exporter->process('storage/work/dump.sql');
} catch (Exception $e) {
    echo 'db-dumper error: ' . $e->getMessage();
}

$exporter = new Exporter($pdo, 'database');
    
$exporter->transformTableRow(function (string $tableName, array $row) {
    if ($tableName === 'customers') {
        $row['social_security_number'] = (string) rand(1000000, 9999999);
    }

    return $row;
});

$exporter->process('storage/work/dump.sql');

$exporter->onTableExport(function($tableName, $rowCount) {
    echo "Exporting table $tableName with $rowCount rows\n";
});

$exporter->setTableWheres([
    'users' => 'date_registered > NOW() - INTERVAL 3 MONTH AND deleted=0',
    'logs' => 'date_logged > NOW() - INTERVAL 1 DAY',
    'posts' => 'active=1'
]);

$exporter->setTableLimits([
    'users' => 300,
    'logs' => 50,
    'posts' => 10
]);

use Dimtrovich\DbDumper\Importer;
use Exception;

try {
    $importer = new Importer($pdo, 'database');
    
    $importer->process('storage/work/dump.sql');
} catch (Exception $e) {
    echo 'db-dumper error: ' . $e->getMessage();
}

$importer->onTableCreate(function($tableName) {
    echo "Creating table $tableName\n";
});
$importer->onTableInsert(function($tableName, $rowCount) {
    echo "Inserting $rowCount rows into table $tableName\n";
});